简体   繁体   English

未定义对构造函数的参数引用

[英]undefined reference to constructor for argument

Good evening 晚上好

I have a small problem with a constructor. 我有一个构造函数的小问题。 I'm trying to build a tree of the different possible plays of my board (to do a DepthFirstSearch after). 我正在尝试为董事会的不同可能角色构建一棵树(之后进行DepthFirstSearch)。 In my node class constructor, i want to copy the current board as a leaf for the tree. 在我的节点类构造函数中,我想复制当前的板作为树的叶子。 But i got an error "undefined reference to `Board::Board()'|" 但是我收到一个错误“未定义对`Board :: Board()'|的引用” when i try to use my Board class instance as an argument for Node constructor. 当我尝试使用我的Board类实例作为Node构造函数的参数时。

If you have an idea of how to do this correctly, i'm listening, i really don't see any problem :( 如果您对如何正确执行此操作有任何想法,我在听,我真的看不到任何问题:(

Here is my class node : 这是我的课程节点:

class Node
{
private :
    Board      state;
    list<Node>  sons;
public :
                Node(Board&);
    void        addNode(Board&);
};

While doing the Node constructor, i do this : 在执行Node构造函数时,我这样做:

Node::Node(Board& tab)
{
    state = tab;
    sons = NULL;
}

My Board class is : 我的董事会课程是:

class Board {

private :
    int**           tab;
    int             nbline;
    int             nbcolumn;
    Position        emptyspot;

public  :
                    Board(int, int, Play&); // initialised with random positions
                    Board(int, int); // just the good 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 to people asking, here are the 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);

       for (int i = 0; i < 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}  {
      for (int i = 0; i < m; ++i){
        tab[i] = new int[n];

        for(int j = 0; j < n; ++j) {
            tab[i][j] = 0;}}
}

As state is not in Node 's constructor initialization list, it will be created with its default constructor. 由于state不在Node的构造函数初始化列表中,因此将使用其默认构造函数创建状态。 The problem is, Board has no default constructor. 问题是, Board没有默认构造函数。 To fix the problem, use the initialization list. 要解决此问题,请使用初始化列表。 This assumes that Board has an appropriate copy constructor. 假设Board具有适当的复制构造函数。

Node::Node(const Board& tab)
    : state(tab)
{
}

I made the parameter const. 我将参数设为const。 I also removed the list assignment. 我还删除了列表分配。 I don't know what it means to assign NULL to a list, but if it's std::list then its default constructor will create it empty anyway. 我不知道将NULL分配给列表是什么意思,但是如果它是std::list那么它的默认构造函数无论如何都会将其创建为空。

It is not strange. 这并不奇怪。 In Node::Node(Board& tab) state is default-initialized before you make an assignment. 在进行分配之前, Node::Node(Board& tab)状态是默认初始化的。 Since you doesn't do it explicitly, compiler calls Board::Board() , which is not defined. 由于您未明确进行此操作,因此编译器将调用未定义的Board::Board() Change Node::Node() to: Node::Node()更改为:

Node::Node(Board& tab)
: state(tab)
, sons(NULL)
{
}

Remember, it is required, that all members must be fully-initialized before body of the constructor gets executed. 请记住,在执行构造函数的主体之前,必须对所有成员进行完全初始化。 So classes containing reference members or instances of classes, that do not have default/copy constructor defined, always must initialize them via initialization list. 因此,包含引用成员或类实例的类(未定义默认/复制构造函数)必须始终通过初始化列表对其进行初始化。

Also, if you have defined operator=() in Board , define also copy constructor (rememeber about Rule of Three ): 另外,如果您在Board定义了operator=() ,那么还请定义复制构造函数(有关“三则规则”的注意事项 ):

class Board
{
//...

  Board(const Board&)
  {
    //make a copy
  }

};

EDIT 编辑

I think Board's copy constructor should be implemented this way: 我认为董事会的副本构造函数应通过以下方式实现:

Board(const Board& origin)
: tab(NULL)
, nbline(origin.nbline)
, nbcolumn(origin.nbcolumn)
, emptyspot(origin.emptyspot)
{
  this->tab = new int*[this->nbline];

  for (int i = 0; i < m; ++i)
    this->tab[i] = new int[this->nbcolumn];

  //Tab contains int's, so let's do simple memcpy()
  memcpy(this->tab, origin.tab, sizeof(int) * this->nbline * this->nbcolumn);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM