Archived

This topic is now archived and is closed to further replies.

Include Troubles

This topic is 5002 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whats the particular problem against this: (MSVS.NET) I used to be able to do this normally, I''m guessing that the defines are somehow global and the classes contained in the headers are not??? /* file1.h */ #ifndef def1 #define def1 #include "file2.h" class ff1 { public: int something; ff2 *pff2; }; #endif /* file2.h */ #ifndef def2 #define def2 #include "file1.h" class ff2 { public: int something; ff1 *pff1; }; #endif ( file2.h(11) : error C2143: syntax error : missing '';'' before ''*'' file2.h(11) : error C2501: ''ff2::ff1'' : missing storage-class or type specifiers file2.h(11) : error C2501: ''ff2:ff1'' : missing storage-class or type specifiers )

Share this post


Link to post
Share on other sites
Those files look correct: did you copy/paste them into your post, or did you retype? My guess is that in your real code you forgot to put a semi-colon at the closing bracket of class ff1 in file1.h. When you create a class, you MUST close it with a semi-colon, even though MSVC doesn''t warn you if you forget, but instead gives weird errors like the one you''re describing.

Share this post


Link to post
Share on other sites
This is entirely correct. In a file that uses file1.h, the #include file1.h expands to:

#ifndef def1
#define def1

//#include "file2.h" expands to:

/* file2.h */

#ifndef def2
#define def2

//#include "file1.h" expands to nothing, as def1 is already defined


class ff2
{
public:
int something;

ff1 *pff1;
};

#endif

class ff1
{
public:
int something;

ff2 *pff2;
};

#endif

To get it to work, put a forward declaration of ff2 in file1.h. like this:

#ifndef def1
#define def1

#include "file2.h"

class ff2;

class ff1
{
public:
int something;

ff2 *pff2;
};
#endif

and the other way around in file2.h

Share this post


Link to post
Share on other sites