Reputation: 500
I have this situation:
class GEngine {
private:
ChessBoard board;
ChessMoves checkBoard(board);
...
and ChessMoves c'tor is defined like this:
class ChessMoves {
private:
ChessBoard nullChessBoard;
ChessBoard const& refBoard;
Flags cBoard[8][8]; //checkboard
public:
ChessMoves():refBoard(nullChessBoard){resetcBoard();}
ChessMoves(ChessBoard const& ref):refBoard(ref){resetcBoard();};
but it return this error:
GameEngine.cpp:250: error: ‘((GEngine*)this)->GEngine::checkBoard’ does not have class type
GameEngine.cpp:251: error: ‘((GEngine*)this)->GEngine::checkBoard’ does not have class type
GameEngine.cpp:251: error: ‘((GEngine*)this)->GEngine::checkBoard’ does not have class type
when I run this code:
//check if piece can move there
checkBoard.fillcBoard(from); //checkBoard called here...
if(checkBoard.get(to) == canPASS or checkBoard.get(to) == canEAT){
/*empty*/
}
else{
return isValid = false;
}
return isValid = true;
I want to pass as a reference to an other class, and initialize is member:
something like
class X{
private:
ref const& _Mreference;
public:
X( ref const& ref):_Mreference(ref){}
};
is it possible?
Upvotes: 2
Views: 151
Reputation: 103713
Your declaration of checkboard is not valid. You cannot initialize an object in the class like that(at least, not in C++03, and no compiler I know of supports the C++11 feature). You need to do it in the constructor, like this:
class GEngine {
private:
ChessBoard board;
ChessMoves checkBoard;
public:
GEngine()
:checkBoard(board)
{}
};
Upvotes: 1
Reputation: 7164
When you do this
ChessMoves checkBoard(board);
you are declaring a function within the class scope GEngine
. This is also an invalid declaration (because board isn't a type - have a look for other error messages from you compiler). When you do a
checkBoard.fillcBoard(from);
your compiler thinks you what to access a member function of the declared function, which is illegal because GEngine::checkBoard
is not a class type (but a function type, and thats what the compiler says).
What you want to do is this:
class GEngine {
private:
ChessBoard board;
ChessMoves checkBoard;
...
// In the implementation file
GEngine::GEngine() : board(), checkBoard(board) {}
And it should be fine. Just be sure that you (in this case) never change the order of declaration within the class, as class members are initialized in the order of their declaration (but if you mix it up, your compiler should warn you about that - be sure to take that warning seriously).
Upvotes: 3