Cyclic dependency - structures and function pointer referring to each other

I am unable to compile the following program due to the circular dependency between struct and function pointer.

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

#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;
}

      

I am getting the following errors:

Error 7 error C2039: 'sFunPtr': not a member of 'gInStruct' c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 31

Error 3 error C2062: enter "int" unexpectedly c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 7

Error 1 error C2065: 'gStruct': undeclared identifier c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 7

Error 2 error C2065: 'pStr': undeclared identifier c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 7

Error 4 error C2146: syntax error: missing ';' before identifier 'sFunPtr' c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

Error 5 Error C4430: Missing type specifier - int. Note: C ++ does not support default-int c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

Error error 6: C4430: Missing type specifier - int. Note: C ++ does not support default-int c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

If I comment out the typedef first and uncomment the second typedef

Error 4 error C2039: 'sFunPtr': not a member of 'gInStruct' c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 31

Error 1 error C2146: syntax error: missing ';' before identifier 'sFunPtr' c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

Error 2 of error C4430: Missing type specifier - int. Note: C ++ does not support default-int c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

Error 3 of error C4430: Missing type specifier - int. Note: C ++ does not support default-int c: \ users \ saurabh17g \ documents \ visual studio 2010 \ projects \ fnptr \ fnptr \ fnptr.cpp 10

Please let me know a possible answer. Thanks in advance, Saurabh

+3


source to share


2 answers


You must declare a struct before you can use it.



Just add struct gStruct;

before typedef

and you will be fine.

+3


source


// 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;
}

      



0


source







All Articles