SIMEL
SIMEL

Reputation: 8939

Problem with class definitions in C++

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?

Upvotes: 2

Views: 121

Answers (2)

rambo
rambo

Reputation: 417

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

using <namespace>:: Board;

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507373

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.

Upvotes: 7

Related Questions