Include Troubles

Started by
4 comments, last by Nerusai 20 years ago
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 )
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
Advertisement
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.
Brianmiserere nostri Domine miserere nostri
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.
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
http://members.home.nl/siaon/Duh.rar
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
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 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
Ah I see now! Thanks!
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.

This topic is closed to new replies.

Advertisement