Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to make other classes to see each other?

Hi all. I have a problem. When I tried to compile following code some strange errors appear as 'Piece::getPosition() not the member of Piece. Because Piece is not defined'

If any one can help me I will be very pleased.

#include
#include

#define ROW_MAX 8
#define COLUMN_MAX 8
#define WHITE_NORMAL_VALUE 2
#define BLACK_NORMAL_VALUE -2
#define WHITE_KING_VALUE 3
#define BLACK_KING_VALUE -3

typedef enum{WHITE,BLACK} COLOR;
typedef enum{NORTH,SOUTH,EAST,WEST} DIRECTION;

class Coordinate;
class Piece;
class Move;
class Board;
class State;

using namespace std;
//=====================================================================
class Coordinate
{
private: int row;
int column;

public: Coordinate(int x=0,int y=0)
{
column=x;
row=y;
}

int getRow()
{
return row;
}

int getColumn()
{
return column;
}

void Set(int x,int y)
{
row=y; column=x;
}

void setRow(int y)
{
row=y;
}

void setColumn(int x)
{
column=x;
}

bool isValidCoordinate()
{
if(row < 1 || row > 8) return false;
if(column < 1 || column > 8) return false;

return true;
}

Coordinate getNextDirection(DIRECTION dir)
{
Coordinate next(column,row);

switch(dir)
{
case NORTH: next.setRow(next.getRow() + 1); break;
case SOUTH: next.setRow(next.getRow() - 1); break;
case EAST: next.setColumn(next.getColumn() + 1); break;
case WEST: next.setColumn(next.getColumn() - 1); break;
}

if(next.isValidCoordinate()) return next;
else return *this; // if there is no next, return it self

}

bool operator==(Coordinate c)
{
if(row==c.getRow() && column==c.getColumn()) return true;

return false;
}

const Coordinate operator=(Coordinate c)
{
if(this!= &c)
{
row=c.getRow();
column=c.getColumn();
}

return *this;
}
};

//=====================================================================

class State
{
public:
virtual bool findValidMoves(Piece *p, Board b , list *MoveList);
virtual bool isKing();
};
//=====================================================================
class NormalWhite : public State
{
public:

bool findValidMoves(Piece *p, Board b, list *MoveList)
{
Coordinate c_tmp;
Move m_tmp;

//check NORTH first
c_tmp = p->getPosition().getNextDirection(NORTH);
if(c_tmp!=p->getPosition()) // if there is no next, return it self
{
if(b.isFieldEmpty(c_tmp)) // if not empty, cant move
{
m_tmp.setDestination(c_tmp),
m_tmp.setPosition(p->getPosition());
MoveList->push_back(m_tmp);
}

}


//delete p
return false;
}

bool isKing()
{
return false;
}


};
//=====================================================================
class King: public State
{
public:
bool findValidMoves( Piece *p, Board b, list *MoveList)
{
//delete p
return false;
}

bool isKing()
{
return true;
}

};
//=====================================================================
class NormalBlack : public State
{
public:

bool findValidMoves( Piece *p, Board b, list *MoveList)
{
//delete p
return false;
}

bool isKing()
{
return false;
}

};

//=====================================================================
class Piece
{
protected: Coordinate position;
State *state;
int value;

public: virtual COLOR getColor();
virtual void makeKing();
virtual Piece * copy();

Coordinate getPosition()
{
return position;
}

bool isKing()
{
return state->isKing();
}

void setPosition( Coordinate c)
{
position=c;
}

int getValue()
{
return value;
}

bool findValidMoves(Board b ,list *MoveList)
{
return state->findValidMoves(this->copy(),b,MoveList);
}

~Piece()
{
delete state;
}

bool operator==( Piece p)
{
return (position==p.getPosition());
}



};
//=====================================================================
class WhitePiece : public Piece
{
public:

WhitePiece(Coordinate c)
{
position=c;
value = WHITE_NORMAL_VALUE;
state= new NormalWhite();
}

void makeKing()
{
delete state;
state=new King();
value=WHITE_KING_VALUE;
}

COLOR getColor()
{
return WHITE;
}

Piece* copy()
{
Piece *newPiece=new WhitePiece(position);
if(isKing())
newPiece->makeKing();

return newPiece;
}
};


//=====================================================================
class BlackPiece : public Piece
{
public:

BlackPiece(Coordinate c)
{
position=c;
value = BLACK_NORMAL_VALUE;
state= new NormalBlack();
}

void makeKing()
{
delete state;
state=new King();
value=BLACK_KING_VALUE;
}

COLOR getColor()
{
return BLACK;
}

Piece* copy()
{
Piece *newPiece=new BlackPiece(position);
if(isKing())
newPiece->makeKing();

return newPiece;
}

};


//=====================================================================

class Board
{
private: list *Pieces;
COLOR turn;
int white_score,black_score,total_score;

public: Board()
{
white_score=black_score=total_score=0;
Pieces=new list ;
reset();
}


void reset()
{
// initialize Board for new game
white_score=black_score=total_score=0;
Coordinate c(3,5);
Pieces->push_back(new WhitePiece(c));
}

~Board()
{
list::iterator p= Pieces->begin();


for(; p!=Pieces->end(); p++) delete *p;

delete Pieces;
}


bool isEmptyField( Coordinate c)
{
list::iterator p= Pieces->begin();

for(; p!=Pieces->end(); p++)
if((*p)->getPosition()==c) return false;

return true;
}

bool isField( Coordinate c, COLOR col)
{
list::iterator p= Pieces->begin();

if(isEmptyField(c)) return false;

for(; p!=Pieces->end(); p++)
if((*p)->getPosition() == c) break;

return( (*p)->getColor()== col );
}


int evaluate()
{
list::iterator p= Pieces->begin();

white_score=black_score=total_score=0;

for(; p!=Pieces->end(); p++)
{
if((*p)->getColor()==WHITE) white_score += (*p)->getValue();
if((*p)->getColor()==BLACK) black_score +=(*p)->getValue();
}

total_score=white_score + black_score;
return total_score;
}

int get_total_score()
{
return total_score;
}

Piece getPiece( Coordinate c)
{
list::iterator p = Pieces->begin();

for(; p!=Pieces->end(); p++)
{
if((*p)->getPosition() == c)
return **p;
}

}

bool remove_piece( Coordinate c)
{
list::iterator p = Pieces->begin();
bool match=false;

for(; p!=Pieces->end(); p++)
{
if((*p)->getPosition() == c)
{
delete *p;
*p=0;
match=true;
break;
}
}
if(match)
Pieces->remove(0); // list de null olanı siler

return match;

}


};

//=====================================================================

class Move
{
private: Coordinate destination;
Coordinate position;

public: Move() {}

Move(Piece p, Coordinate c)
{
destination=c;
position=p.getPosition();
}

Move(Coordinate p, Coordinate c)
{
destination=c;
position=p;
}

Coordinate getDestination()
{
return destination;
}

Coordinate getPosition()
{
return position;
}

void setDestination( Coordinate c)
{
destination=c;
}

void setPosition(Coordinate c)
{
position=c;
}
};

//=====================================================================

int main()
{
system("pause");
return 0;
}

//=====================================================================

Comments

  • tom_swtom_sw Member Posts: 828
    Visual Studio 6 compiled this jsut fine. What toolsa re you using? And did you compile this as a C++ project or a C project?

    : Hi all. I have a problem. When I tried to compile following code some strange errors appear as 'Piece::getPosition() not the member of Piece. Because Piece is not defined'
    :
    : If any one can help me I will be very pleased.
    : [code]
    : #include
    : #include
    :
    : #define ROW_MAX 8
    : #define COLUMN_MAX 8
    : #define WHITE_NORMAL_VALUE 2
    : #define BLACK_NORMAL_VALUE -2
    : #define WHITE_KING_VALUE 3
    : #define BLACK_KING_VALUE -3
    :
    : typedef enum{WHITE,BLACK} COLOR;
    : typedef enum{NORTH,SOUTH,EAST,WEST} DIRECTION;
    :
    : class Coordinate;
    : class Piece;
    : class Move;
    : class Board;
    : class State;
    :
    : using namespace std;
    : //=====================================================================
    : class Coordinate
    : {
    : private: int row;
    : int column;
    :
    : public: Coordinate(int x=0,int y=0)
    : {
    : column=x;
    : row=y;
    : }
    :
    : int getRow()
    : {
    : return row;
    : }
    :
    : int getColumn()
    : {
    : return column;
    : }
    :
    : void Set(int x,int y)
    : {
    : row=y; column=x;
    : }
    :
    : void setRow(int y)
    : {
    : row=y;
    : }
    :
    : void setColumn(int x)
    : {
    : column=x;
    : }
    :
    : bool isValidCoordinate()
    : {
    : if(row < 1 || row > 8) return false;
    : if(column < 1 || column > 8) return false;
    :
    : return true;
    : }
    :
    : Coordinate getNextDirection(DIRECTION dir)
    : {
    : Coordinate next(column,row);
    :
    : switch(dir)
    : {
    : case NORTH: next.setRow(next.getRow() + 1); break;
    : case SOUTH: next.setRow(next.getRow() - 1); break;
    : case EAST: next.setColumn(next.getColumn() + 1); break;
    : case WEST: next.setColumn(next.getColumn() - 1); break;
    : }
    :
    : if(next.isValidCoordinate()) return next;
    : else return *this; // if there is no next, return it self
    :
    : }
    :
    : bool operator==(Coordinate c)
    : {
    : if(row==c.getRow() && column==c.getColumn()) return true;
    :
    : return false;
    : }
    :
    : const Coordinate operator=(Coordinate c)
    : {
    : if(this!= &c)
    : {
    : row=c.getRow();
    : column=c.getColumn();
    : }
    :
    : return *this;
    : }
    : };
    :
    : //=====================================================================
    :
    : class State
    : {
    : public:
    : virtual bool findValidMoves(Piece *p, Board b , list *MoveList);
    : virtual bool isKing();
    : };
    : //=====================================================================
    : class NormalWhite : public State
    : {
    : public:
    :
    : bool findValidMoves(Piece *p, Board b, list *MoveList)
    : {
    : Coordinate c_tmp;
    : Move m_tmp;
    :
    : //check NORTH first
    : c_tmp = p->getPosition().getNextDirection(NORTH);
    : if(c_tmp!=p->getPosition()) // if there is no next, return it self
    : {
    : if(b.isFieldEmpty(c_tmp)) // if not empty, cant move
    : {
    : m_tmp.setDestination(c_tmp),
    : m_tmp.setPosition(p->getPosition());
    : MoveList->push_back(m_tmp);
    : }
    :
    : }
    :
    :
    : //delete p
    : return false;
    : }
    :
    : bool isKing()
    : {
    : return false;
    : }
    :
    :
    : };
    : //=====================================================================
    : class King: public State
    : {
    : public:
    : bool findValidMoves( Piece *p, Board b, list *MoveList)
    : {
    : //delete p
    : return false;
    : }
    :
    : bool isKing()
    : {
    : return true;
    : }
    :
    : };
    : //=====================================================================
    : class NormalBlack : public State
    : {
    : public:
    :
    : bool findValidMoves( Piece *p, Board b, list *MoveList)
    : {
    : //delete p
    : return false;
    : }
    :
    : bool isKing()
    : {
    : return false;
    : }
    :
    : };
    :
    : //=====================================================================
    : class Piece
    : {
    : protected: Coordinate position;
    : State *state;
    : int value;
    :
    : public: virtual COLOR getColor();
    : virtual void makeKing();
    : virtual Piece * copy();
    :
    : Coordinate getPosition()
    : {
    : return position;
    : }
    :
    : bool isKing()
    : {
    : return state->isKing();
    : }
    :
    : void setPosition( Coordinate c)
    : {
    : position=c;
    : }
    :
    : int getValue()
    : {
    : return value;
    : }
    :
    : bool findValidMoves(Board b ,list *MoveList)
    : {
    : return state->findValidMoves(this->copy(),b,MoveList);
    : }
    :
    : ~Piece()
    : {
    : delete state;
    : }
    :
    : bool operator==( Piece p)
    : {
    : return (position==p.getPosition());
    : }
    :
    :
    :
    : };
    : //=====================================================================
    : class WhitePiece : public Piece
    : {
    : public:
    :
    : WhitePiece(Coordinate c)
    : {
    : position=c;
    : value = WHITE_NORMAL_VALUE;
    : state= new NormalWhite();
    : }
    :
    : void makeKing()
    : {
    : delete state;
    : state=new King();
    : value=WHITE_KING_VALUE;
    : }
    :
    : COLOR getColor()
    : {
    : return WHITE;
    : }
    :
    : Piece* copy()
    : {
    : Piece *newPiece=new WhitePiece(position);
    : if(isKing())
    : newPiece->makeKing();
    :
    : return newPiece;
    : }
    : };
    :
    :
    : //=====================================================================
    : class BlackPiece : public Piece
    : {
    : public:
    :
    : BlackPiece(Coordinate c)
    : {
    : position=c;
    : value = BLACK_NORMAL_VALUE;
    : state= new NormalBlack();
    : }
    :
    : void makeKing()
    : {
    : delete state;
    : state=new King();
    : value=BLACK_KING_VALUE;
    : }
    :
    : COLOR getColor()
    : {
    : return BLACK;
    : }
    :
    : Piece* copy()
    : {
    : Piece *newPiece=new BlackPiece(position);
    : if(isKing())
    : newPiece->makeKing();
    :
    : return newPiece;
    : }
    :
    : };
    :
    :
    : //=====================================================================
    :
    : class Board
    : {
    : private: list *Pieces;
    : COLOR turn;
    : int white_score,black_score,total_score;
    :
    : public: Board()
    : {
    : white_score=black_score=total_score=0;
    : Pieces=new list ;
    : reset();
    : }
    :
    :
    : void reset()
    : {
    : // initialize Board for new game
    : white_score=black_score=total_score=0;
    : Coordinate c(3,5);
    : Pieces->push_back(new WhitePiece(c));
    : }
    :
    : ~Board()
    : {
    : list::iterator p= Pieces->begin();
    :
    :
    : for(; p!=Pieces->end(); p++) delete *p;
    :
    : delete Pieces;
    : }
    :
    :
    : bool isEmptyField( Coordinate c)
    : {
    : list::iterator p= Pieces->begin();
    :
    : for(; p!=Pieces->end(); p++)
    : if((*p)->getPosition()==c) return false;
    :
    : return true;
    : }
    :
    : bool isField( Coordinate c, COLOR col)
    : {
    : list::iterator p= Pieces->begin();
    :
    : if(isEmptyField(c)) return false;
    :
    : for(; p!=Pieces->end(); p++)
    : if((*p)->getPosition() == c) break;
    :
    : return( (*p)->getColor()== col );
    : }
    :
    :
    : int evaluate()
    : {
    : list::iterator p= Pieces->begin();
    :
    : white_score=black_score=total_score=0;
    :
    : for(; p!=Pieces->end(); p++)
    : {
    : if((*p)->getColor()==WHITE) white_score += (*p)->getValue();
    : if((*p)->getColor()==BLACK) black_score +=(*p)->getValue();
    : }
    :
    : total_score=white_score + black_score;
    : return total_score;
    : }
    :
    : int get_total_score()
    : {
    : return total_score;
    : }
    :
    : Piece getPiece( Coordinate c)
    : {
    : list::iterator p = Pieces->begin();
    :
    : for(; p!=Pieces->end(); p++)
    : {
    : if((*p)->getPosition() == c)
    : return **p;
    : }
    :
    : }
    :
    : bool remove_piece( Coordinate c)
    : {
    : list::iterator p = Pieces->begin();
    : bool match=false;
    :
    : for(; p!=Pieces->end(); p++)
    : {
    : if((*p)->getPosition() == c)
    : {
    : delete *p;
    : *p=0;
    : match=true;
    : break;
    : }
    : }
    : if(match)
    : Pieces->remove(0); // list de null olanı siler
    :
    : return match;
    :
    : }
    :
    :
    : };
    :
    : //=====================================================================
    :
    : class Move
    : {
    : private: Coordinate destination;
    : Coordinate position;
    :
    : public: Move() {}
    :
    : Move(Piece p, Coordinate c)
    : {
    : destination=c;
    : position=p.getPosition();
    : }
    :
    : Move(Coordinate p, Coordinate c)
    : {
    : destination=c;
    : position=p;
    : }
    :
    : Coordinate getDestination()
    : {
    : return destination;
    : }
    :
    : Coordinate getPosition()
    : {
    : return position;
    : }
    :
    : void setDestination( Coordinate c)
    : {
    : destination=c;
    : }
    :
    : void setPosition(Coordinate c)
    : {
    : position=c;
    : }
    : };
    :
    : //=====================================================================
    :
    : int main()
    : {
    : system("pause");
    : return 0;
    : }
    :
    : //=====================================================================
    :[/code]

  • v3n0m_85v3n0m_85 Member Posts: 2
    I am using Borland C++ Builder 6. But also I tried Visual C++ 6, not the same errors but it gives some other errors.And Im creating c++ project. Its not compiled fine.
    What could be the problem?
    By the way, thanks for your interest.
    This is my graduation project. So it is very important for me.
  • aitkenaitken Member Posts: 8
    The problem is you must have Piece defined before you use any of its data or function members. In class State you are using it before compiler can see its definition, so you get an error. If you declare but not define a class you can only use it as a type, but you cannot actually construct any objects of this type or use any methods or access data members via pointers or references.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : The problem is you must have Piece defined before you use any of its data or function members. In class State you are using it before compiler can see its definition, so you get an error. If you declare but not define a class you can only use it as a type, but you cannot actually construct any objects of this type or use any methods or access data members via pointers or references.
    :
    [blue]The good way of doing it is always use pointers to create class instances and not use the class aggregation, when you declare an instance of a class inside other class. Consider this:
    [/blue]
    [code]
    class A;
    class B;
    class C;
    // ^^^ Always declare forwards in such code design

    class B {
    private:
    C* pC;
    // ^^^ Compiler knows that its size is 4 bytes (in 32 bit code)
    // This why it compiles OK, however, compiler does not 'know' yet
    // about C internals, it just 'knows' that C is a class
    };

    class C {
    int a [100];
    };
    [/code]
    [blue]Now, lets declare it as a full instance:
    [/blue]
    [code]
    class A;
    class B;
    class C;

    class B {
    private:
    C objC;
    // ^^^ This will not compile because, compiler does not 'know' yet
    // about the size of C and size is needed to cinstuct the
    // size of B
    };

    class C {
    int a [100];
    };
    [/code]
    [blue]You will be forced to move class C BEFORE class B:
    [/blue]
    [code]
    class A;
    class B;
    class C;

    class C {
    int a [100];
    };

    class B {
    private:
    C objC;
    // ^^^ This will compile fine, compiler 'knows'
    // about the size of C
    };
    [/code]
    [blue]However, the more complex your code will get - the more problems you hit with aggregation of classes and it will be a time when the project will not compile no matter what you do, because aggregated classes will drag around its dependencies and no matter how you try to 'fix' it (by placing classes before other ones) it will not be compiled. The only way is to use pointers to classes and use new operator to instantiate classes in constructors or in other methods. Also, you may think of declaring the class references, but there may be a problem with initializing these refs.[/blue]

Sign In or Register to comment.