Tuesday, 15 April 2014

pointer to a struct inside of a struct from another file in c++ -



pointer to a struct inside of a struct from another file in c++ -

i need create programme deed stack. did function correctly. problem have 2 structs in 2 files when i'm trying set pointer other struct won't allow me.

the first struct declared in file "linkedlist.h":

#ifndef _linkedlist_h #define _linkedlist_h #include "stack.h" struct elements{ int element; elements* pnext; }; typedef struct elements elements; void push(mystack *s, int element); // insert element top of stack int pop(mystack *s); //remove element top of stack #endif

the sec struct declared in sec file "stack.h":

#ifndef _mystack_h #define _mystack_h #include "linkedlist.h" struct mystack{ int maxsize; int count; bool empty; elements* firstelement; //the problem in line********************* }; typedef struct mystack mystack; void initstack(mystack *s, int size); void cleanstack(mystack *s); bool isempty(mystack *s); bool isfull(mystack *s); #endif

but when trying compile it gives me error:

error c2143: syntax error : missing ';' before '*'. error c4430: missing type specifier - int assumed. note: c++ not back upwards default-int

again error points line of code:

elements* firstelement;

how prepare this?

linkedlist.h includes stack.h , stack.h includes linkedlists.h....you cannot that. then, #ifndef _linkedlist_h/#define _linkedlist_h makes elements not defined in end...

you need remove circular dependency using forwards declaration, below.

change linkedlist.h by:

#ifndef _linkedlist_h #define _linkedlist_h //#include "stack.h" struct mystack; struct elements{ int element; elements* pnext; }; typedef struct elements elements; void push(mystack *s, int element); // insert element top of stack int pop(mystack *s); //remove element top of stack #endif

you can create 3rd file functions definitions, there's many ways prepare that.

c++ pointers struct

No comments:

Post a Comment