c++ - undefined reference to constructor for argument -
good evening
i have small problem constructor. i'm trying build tree of different possible plays of board (to depthfirstsearch after). in node class constructor, want copy current board leaf tree. got error "undefined reference `board::board()'|" when try use board class instance argument node constructor.
if have idea of how correctly, i'm listening, don't see problem :(
here class node :
class node { private : board state; list<node> sons; public : node(board&); void addnode(board&); };
while doing node constructor, :
node::node(board& tab) { state = tab; sons = null; }
my board class :
class board { private : int** tab; int nbline; int nbcolumn; position emptyspot; public : board(int, int, play&); // initialised random positions board(int, int); // size, no values inside. node::node during constructor. void setvalue(position&, int); void setnbline(int m); void setnbcolumn(int n); int getvalue(position&); int getnbline(); int getnbcolumn(); int getemptyline(); int getemptycolumn(); void setemptyspot(position&); position& getemptyspot(); board& operator=(board& source); };
edit : due people asking, here constructors of board:
board::board(int m, int n, play& jeu) : tab{new int*[m]}, nbline{m}, nbcolumn{n}, emptyspot{n-1,m-1}{ int x(1); (int = 0; < m; ++i){ tab[i] = new int[n]; for(int j = 0; j < n; ++j) { tab[i][j] = x; x++;}} tab[n-1][m-1]=0; x=0; while (x!=1000) { int numbers[] = { up, down, left, right }; int length = sizeof(numbers) / sizeof(int); int randomnumber = numbers[rand() % length]; jeu.moves(*this, randomnumber); x++; } } board::board(int m, int n) : tab{new int*[m]}, nbline{m}, nbcolumn{n}, emptyspot{n-1,m-1} { (int = 0; < m; ++i){ tab[i] = new int[n]; for(int j = 0; j < n; ++j) { tab[i][j] = 0;}} }
it not strange. in node::node(board& tab)
state default-initialized before make assignment. since doesn't explicitly, compiler calls board::board()
, not defined. change node::node()
to:
node::node(board& tab) : state(tab) , sons(null) { }
remember, required, members must fully-initialized before body of constructor gets executed. classes containing reference members or instances of classes, not have default/copy constructor defined, must initialize them via initialization list.
also, if have defined operator=()
in board
, define copy constructor (rememeber rule of three):
class board { //... board(const board&) { //make copy } };
edit
i think board's copy constructor should implemented way:
board(const board& origin) : tab(null) , nbline(origin.nbline) , nbcolumn(origin.nbcolumn) , emptyspot(origin.emptyspot) { this->tab = new int*[this->nbline]; (int = 0; < m; ++i) this->tab[i] = new int[this->nbcolumn]; //tab contains int's, let's simple memcpy() memcpy(this->tab, origin.tab, sizeof(int) * this->nbline * this->nbcolumn); }
Comments
Post a Comment