循环依赖 - 结构和函数指针相互引用

时间:2012-04-02 02:31:10

标签: c structure function-pointers cyclic-reference

由于结构和函数指针之间的循环依赖,我无法编译以下程序。

// fnPtr.cpp:定义控制台应用程序的入口点。 //

#include "stdafx.h"
#include "stdlib.h"
//typedef gNewType;
typedef void (*ptr)(gStruct *pStr, int pNm);
struct gInStruct{
char *sAlpBet;
ptr sFunPtr;    
};

struct gStruct{
//gNewType sNtype;
gInStruct sInStruct;
int sNum;
};
//typedef void (*ptr)(gStruct *pStr, int pNm);
void myFun(char *pStr1, int pNum){

return ;
}
int _tmain(int argc, _TCHAR* argv[])
{
void (*fnPtr)(char *pStr, int pNm);
char lAlphabet = 'a';
fnPtr = myFun;
gStruct *lStructPtr = (gStruct*)malloc(sizeof(gStruct));
lStructPtr->sNum = 10;
lStructPtr->sInStruct.sAlpBet = &lAlphabet;
lStructPtr->sInStruct.sFunPtr = fnPtr;
return 0;
}

我收到以下错误:

错误7错误C2039:'sFunPtr':不是'gInStruct'的成员c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 31

错误3错误C2062:输入'int'意外c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 7

错误1错误C2065:'gStruct':未声明的标识符c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 7

错误2错误C2065:'pStr':未声明的标识符c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 7

错误4错误C2146:语法错误:缺少';'在标识符'sFunPtr之前'c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

错误5错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

错误6错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

如果我先评论typedef并取消注释第二个typedef

错误4错误C2039:'sFunPtr':不是'gInStruct'的成员c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 31

错误1错误C2146:语法错误:缺少';'在标识符'sFunPtr之前'c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

错误2错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

错误3错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int c:\ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

请告诉我一个可能的答案。 提前致谢, SAURABH

2 个答案:

答案 0 :(得分:3)

您应该在使用它之前声明一个结构。

只需在struct gStruct;之前添加typedef,就可以了。

答案 1 :(得分:0)

// fnPtr.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
//typedef gNewType;
struct gStruct;
typedef void (*ptr)(gStruct *pStr, int pNm);
struct gInStruct{
char *sAlpBet;
ptr sFunPtr;    
};

struct gStruct{
//gNewType sNtype;
gInStruct sInStruct;
int sNum;
};
//typedef void (*ptr)(gStruct *pStr, int pNm);
void myFun(gStruct *pStr1, int pNum){

return ;
}
int _tmain(int argc, _TCHAR* argv[])
{
void (*fnPtr)(gStruct *pStr, int pNm);
char lAlphabet = 'a';
fnPtr = myFun;
gStruct *lStructPtr = (gStruct*)malloc(sizeof(gStruct));
lStructPtr->sNum = 10;
lStructPtr->sInStruct.sAlpBet = &lAlphabet;
lStructPtr->sInStruct.sFunPtr = fnPtr;
return 0;
}