VC++ not recognizing 'GameBoard' in code

H

Huyvtq

I have a class Chess with the contructor:
Chess(const GameBoard& argGameBoard,int,int,Color,int); //line 1
and a property:
GameBoard& mGameBoard; //line 2

to creat a reference to one obj of
GameBoard: :mGameBoard(argGameBoard) at constructor.

But when It's compiled (by VC2005++). This errors occured:
at line 1

1>e:\vc2005\chessgame\src\Chess.h(34) : error C2059: syntax error :
'<cv-qualifer>'
1>e:\vc2005\chessgame\src\Chess.h(34) : error C2238: unexpected
token(s) preceding ';'



and at line 2:
1>e:\vc2005\chessgame\src\Chess.h(69) : error C2143: syntax error :
missing ';' before '&'
1>e:\vc2005\chessgame\src\Chess.h(69) : error C4430: missing type
specifier - int assumed. Note: C++ does not support default-int

It seem that Compiler don't know what GameBoard is in class Chess.
Here class GameBoard I have defined and included. It have a property:
Stack<TurnHistory>& aReport;

Who can tell me what it is?

Tks a lot!
 
A

Alf P. Steinbach

* Huyvtq:
I have a class Chess with the contructor:
Chess(const GameBoard& argGameBoard,int,int,Color,int); //line 1
and a property:
GameBoard& mGameBoard; //line 2

to creat a reference to one obj of
GameBoard: :mGameBoard(argGameBoard) at constructor.

But when It's compiled (by VC2005++). This errors occured:
at line 1

1>e:\vc2005\chessgame\src\Chess.h(34) : error C2059: syntax error :
'<cv-qualifer>'
1>e:\vc2005\chessgame\src\Chess.h(34) : error C2238: unexpected
token(s) preceding ';'



and at line 2:
1>e:\vc2005\chessgame\src\Chess.h(69) : error C2143: syntax error :
missing ';' before '&'
1>e:\vc2005\chessgame\src\Chess.h(69) : error C4430: missing type
specifier - int assumed. Note: C++ does not support default-int

It seem that Compiler don't know what GameBoard is in class Chess.
Here class GameBoard I have defined and included. It have a property:
Stack<TurnHistory>& aReport;

Who can tell me what it is?

The error message says "syntax error". You have a syntax error. Post
the offending code.
 
H

Huyvtq

Chess(const GameBoard& argGameBoard,int,int,Color,int); //line 1

There're clearly no syntax errors in this line.
 
K

Kai-Uwe Bux

Huyvtq said:
Chess(const GameBoard& argGameBoard,int,int,Color,int); //line 1

There're clearly no syntax errors in this line.

Right. If your compiler complains about this line, you should consider
filing a bug report.

Now, on second thought, whether there is an error on that line actually
depends on a lot of context in the ambient code. The line you posted is the
line where the compiler decided it has enough information to report an
error, it is not necessarily the line where the error is located. Maybe,
your compiler is right, but alas, we have no chance to tell given just this
one line.


Best

Kai-Uwe Bux
 
H

Huyvtq

class Chess //abstract Class
{

public:
Chess(const GameBoard& argGameBoard,int,int,Color,int);

virtual bool tryToMove(const Point& aPoint);

void setCurrentPoint(int x,int y);
Point getCurrentPoint() const;

int getColor() const
{
return mColor;
}

int getIdx(){
return mIdx;
}

char getDisplayCharacter(){
return mDisplay;
}

protected:

const GameBoard& mGameBoard;

Point mCurrentPoint;

Color mColor;

char mDisplay;

int mIdx;
};

Chess::Chess(GameBoard& argGameBoard,int x,int y,Color argColor,int
argIdx):mGameBoard(argGameBoard)
{
setCurrentPoint(x,y);
mColor=argColor;
mIdx=argIdx;
}

....................

These're all relative code. Hope you will help me :(
Tks in advantage!
 
I

Ian Collins

Huyvtq said:
class Chess //abstract Class
{

public:
Chess(const GameBoard& argGameBoard,int,int,Color,int);
const GameBoard& here

Chess::Chess(GameBoard& argGameBoard,int x,int y,Color argColor,int
argIdx):mGameBoard(argGameBoard)

Not const here.
{
setCurrentPoint(x,y);
mColor=argColor;
mIdx=argIdx;

Why not use initialisers for these?


Are you sure at least a declaration of GameBoard is visible to the compiler?
 
H

Huyvtq

const GameBoard& here

<snip>




Not const here.


Why not use initialisers for these?

Are you sure at least a declaration of GameBoard is visible to the compiler?

I tried. But it still like that.
The problem seem that Compiler doesnt know what GameBoard is although
I've define & included it
:(
 
H

Huyvtq

Code of GameBoard.h

#ifndef GAMEBOARD_H
#define GAMEBOARD_H

#include "Chess.h"
#include "TurnHistory.h"
#include "Stack.h"
#include <iostream>
using namespace std;

const int kBoardWidth=8;
const int kBoardHeight=8;

class GameBoard
{
public:
GameBoard(Stack<TurnHistory>&);
~GameBoard();
bool move(const Point&,const Point&);
// tro Point den chessArr[int]
void setChess(const Point&,int);
Chess& getChess(const Point&) const;
void drawBoard() const;
bool getIsEnd() const;

private:
Chess* chessPtr[kBoardWidth][kBoardHeight];
Chess* chessArray[32];
bool isEnd;
Stack<TurnHistory>& aReport;
};

#endif
 
I

Ian Collins

Huyvtq said:
Code of GameBoard.h

#ifndef GAMEBOARD_H
#define GAMEBOARD_H

#include "Chess.h"

This doesn't define the Chess class by any chance, does it? If so, all
bets are off and you will have to look at using forward declarations for
each class in the other's header.
using namespace std;
Never, ever do this in a header! Any file that included the header is
stuck with this declaration.
 
H

Huyvtq

Tks so muchhhhhhhhhh ^^^^^^^^^^^^^^^^^^^^^^

I used the forward declarations, and it worked ^^
But I don't know what is different b/t forward declarations and
include?? Why do we have to use forward declarations here?
 
T

tragomaskhalos

Tks so muchhhhhhhhhh ^^^^^^^^^^^^^^^^^^^^^^

I used the forward declarations, and it worked ^^
But I don't know what is different b/t forward declarations and
include?? Why do we have to use forward declarations here?

Because Chess refers to Gameboard and Gameboard refers
to Chess, so you have a cyclic dependency. If you examine
your original code and trace through the #includes you should
be able to see why this doesn't work. A forward declaration
breaks the cycle. Again, trace it through and you'll see.

As a general rule, irrespective of cyclic dependencies,
you should prefer forward declarations in header files
whenever the class in question is only used as a pointer
or reference.
 
R

Ron Natalie

Kai-Uwe Bux said:
Right. If your compiler complains about this line, you should consider
filing a bug report.

Now, on second thought, whether there is an error on that line actually
depends on a lot of context in the ambient code. The line you posted is the
line where the compiler decided it has enough information to report an
error, it is not necessarily the line where the error is located. Maybe,
your compiler is right, but alas, we have no chance to tell given just this
one line.

If that is truly line 1, it's full of erros. Chess and GameBoard
aren't defined anywhere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top