小编Sun*_*Liu的帖子

复制构造函数和赋值运算符都被调用

我有以下程序片段:

Polynomial Polynomial:: add(const Polynomial b)
{
    Polynomial c;
    c.setRoot(internalAdd(root, c.root));
    c.setRoot(internalAdd(b.root, c.root));
    return c;
}

c = (a.add(b));
Run Code Online (Sandbox Code Playgroud)

据我所知,这段代码假设将a和b加在一起,然后通过调用复制构造函数将结果多项式赋值给c.

但是,当我真正测试它时,

  • c立即调用复制构造函数并尝试复制b,
  • 然后a和b添加
  • 然后c尝试通过赋值运算符得到结果多项式
  • 然后程序崩溃了

我该怎么做才能解决这个问题?

c++ constructor copy variable-assignment operator-keyword

1
推荐指数
1
解决办法
188
查看次数

c ++中未定义的结构类型

以下是我的index.h类

class index
{

struct node {
        string item;
        int counter;
        vector<int> pageNumber;
    };

public:
    node* newNode (string word);
    ...

private:
    vector<node*> indexStructure;
    int lineCounter;
    int indexSize;

};
Run Code Online (Sandbox Code Playgroud)

在我的index.cpp类中,我有一个方法定义如下:

node* index::newNode (string word)
{
    node* temp = new node();
    temp.item = word;
    temp.counter = 1;
    temp.pageNumber = new vector <int> (10, -1);
    temp.pageNumber[0] = lineCounter / 40;
    return temp;

}
Run Code Online (Sandbox Code Playgroud)

当我编译时,它告诉我"节点没有命名类型",即使它在index.h中的结构中定义,并且我的私有向量变量可以具有类型node*.

c++ struct types pointers

0
推荐指数
1
解决办法
1121
查看次数