I am trying to build a Hex Game using the Polymorphism principle. I have a class inside an abstract class. I need a 2D vector of Cells (nested class) but I'm getting this error when I try to implement a function to resize my vector.
Base and Abstract Class = AbstractHex || Nested Class = Cell || Derived Class = HexVector
Here's what I got (ERROR):
/usr/bin/ld: /tmp/ccq9PAk3.o: in function `void std::_Construct<HexGame::AbstractHex::Cell>(HexGame::AbstractHex::Cell*)':HexVector.cpp:(.text._ZSt10_ConstructIN7HexGame11AbstractHex4CellEJEEvPT_DpOT0_[_ZSt10_ConstructIN7HexGame11AbstractHex4CellEJEEvPT_DpOT0_]+0x2d): undefined reference to `HexGame::AbstractHex::Cell::Cell()' collect2: error: ld returned 1 exit status
My Base Class (Abstract):
namespace HexGame{
class AbstractHex{
public:
class Cell{
public:
explicit Cell();
explicit Cell(int rw,int col) : row(rw) , column(col){} //INTENTIONALLY EMPTY
explicit Cell(int rw,int col, char p) : row(rw) , column(col) , point(p){}
//INTENTIONALLY EMPTY
int getRow()const {return row;};
int getColumn()const {return column;};
void setRow(int index) {row = index;};
void setColumn(int index) {column = index;};
int getPoint()const {return point;};
void setPoint(char param) {point = param;};
private:
int row;
int column;
char point;
};
virtual void setSize() = 0;
private:
int board_size;
I'm calling setSize() function which is in abstract class. Can I call a virtual function within a non-virtual function which is in abstract class?
void AbstractHex::startGame(){ //non-virtual function in Abstract Class
setSize(); //virtual function in Abstract Class
//I'm calling startGame() function every time I create an object of HexVector
}
My Derived Class:
class HexVector : public AbstractHex{
public:
void setSize() override;
private:
vector<vector<AbstractHex::Cell> > board;
Overriding of setSize() function
void HexVector::setSize(){
board.resize(getBoardSize());
for(int i=0; i<getBoardSize(); i++){
board[i].resize(getBoardSize());
}
}
Aucun commentaire:
Enregistrer un commentaire