1. 程式人生 > >error: expected declaration specifiers or '...' before xxx(xxx是函數形參)

error: expected declaration specifiers or '...' before xxx(xxx是函數形參)

... 編譯 exp 定義 包含 情況 搜索 .... make

在使用帶參有返回值的函數指針做參數時,編譯出現下面情況

……………………

error: expected declaration specifiers or ‘...‘ before ‘FunType‘

情形描述:

a.h:

typedef void (*FunType)();

void callFun(FunType p);

a.c :

#include "a.h"

FunType myfuntype=NULL;

void callFun(FunType p)

{

  myfuntype=p;

}

b.c

#include "a.h"

MyFun()

{

  .....;

}

callFun(MyFun);

函數指針變量FunTypevar 定義在 a.c 中,a.h聲明 了 相應的typedef 和函數 .在b.c 想使用a.c中函數指針,於是在b.h中#include"a.h",最後出現了上面的編譯錯誤。

原因分析:

網上搜索,討論的不少,越看越茫然。

可能原因是

b.h中#include "a.h" 而a.h中的函數聲明中用到了b.h中的結構體或者typedef,那麽就會出現在包含a.h的時候b.h中的結構體或者typedef還沒有聲明,從而陷入錯誤.

解決辦法:

將a.h 刪除

a.c:

typedef void (*FunType)();

FunType myfuntype=NULL;

void callFun(FunType p)

{

  myfuntype=p;

}

b.c

typedef void (*FunType)();

extern void callFun(FunType p);

MyFun()

{

  .....;

}

callFun(MyFun);

當再次make 的時候,pass了

error: expected declaration specifiers or '...' before xxx(xxx是函數形參)