2

I have a class defined in the file board.H:

class Board { private: ... public: ... }; 

and in another class, I want to have a member that is a pointer to a Board object:

#include "board.H" class Bear { private: Board* board; ... public: ... }; 

When I try to compile it (using g++ in linux) I get the following error:

bear.H:15: error: ISO C++ forbids declaration of `Board' with no type bear.H:15: error: expected `;' before '*' token 

What am I doing wrong?

3
  • Is your Board class definition inside a namespace? Commented May 27, 2011 at 16:22
  • 3
    I suspect the error lies in the part of the code you have clipped. Commented May 27, 2011 at 16:24
  • How are you compiling this? I see no problem with your code. Commented May 27, 2011 at 16:24

2 Answers 2

7

Common problem. You probably have a #include "bear.H" line in your "board.H" file or in a file included by "board.H".

So when you include "bear.H" into "board.H", the "bear.H" file is processed and tries to include "board.H", but that file is already being processed so the header guard of "bear.H" won't include the content another time. But then "bear.H" is processed without a leading "Board" class definition.

Sign up to request clarification or add additional context in comments.

3 Comments

@Iiya Melamed I recommend that you make a new question for that. But as a hint - try to not depend on the definition of class Bear in "board.H". For example a "Bear*" doesn't need the definition of class "Bear", but only a forward declaration like "class Bear;". Then you don't need to include the "bear.H" header. Same goes for "void f(Bear b);" (but not "void f(Bear b) { }") and "Bear&".
Compiler directives. By wrapping your classes with the #ifndef, #define, #endif directives found at the following address, the compiler should only try to compile each class once: cprogramming.com/reference/preprocessor/ifndef.html
I love how circular dependency error messages like this are so ambiguous.
0

Check the namespace. If Board is in a different namespace than Bear, you need to add in Bear.h:

using <namespace>:: Board; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.