Include Troubles
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
)
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.
They are exact copies, this was just a simple test to proof that that was the problem and not a typo.
So it''s not a typo.
So it''s not a typo.
This is entirely correct. In a file that uses file1.h, the #include file1.h expands to:
To get it to work, put a forward declaration of ff2 in file1.h. like this:
and the other way around in file2.h
#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 definedclass ff2{public:int something;ff1 *pff1;};#endifclass 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement