类链表的复制构造函数不起作用

Copy Constructor for Class Linked List not working

提问人:swittuth 提问时间:12/30/2020 最后编辑:swittuth 更新时间:12/30/2020 访问量:87

问:

我的复制构造函数引用另一个地址而不是复制值,你们能帮我找到问题吗?

这是复制构造函数:

Poly::Poly(const Poly& copyList)
{

    // copy the first term
    power = copyList.power;
    coef = copyList.coef;

    if (copyList.nextTerm == NULL)
    {
        nextTerm = NULL;
    }
    else
    {
        // copy the next term
        PolyPtr trackCopy = copyList.nextTerm;
        nextTerm = new Poly;
        PolyPtr trackOriginal = nextTerm;
        PolyPtr newTerm;

        while (trackCopy != NULL)
        {
            newTerm = new Poly;
            newTerm->coef = trackCopy->coef;
            newTerm->power = trackCopy->power;
            trackOriginal->nextTerm = newTerm;
            trackOriginal = newTerm;

            trackCopy = trackCopy->nextTerm;

        }   
    }

}

copyList 是一个类,其私有成员变量为 coef、power 和引用列表下一个节点的 nextTerm

以下是 Poly 的界面:

class Poly
{
    public:
        // constructors
        Poly();
        Poly(int constant);
        Poly(int coef, int power);
        Poly(const Poly& copyList);

        // set functions
        void setPower(int number);
        void setCoef(int number);
        void setNextTerm(Poly* link);

        // member function to compute
        int evaluate(int x);


    private:
        int power;
        int coef;
        Poly* nextTerm;

};
typedef Poly* PolyPtr;

下面是一个示例:

int main()
{
    PolyPtr polyNomial = new Poly(2, 3);
    PolyPtr nextTerm = new Poly(5, 8);

    polyNomial->setNextTerm(nextTerm);

    PolyPtr copyTerm(polyNomial);

    PolyPtr newTerm = new Poly(1, 2);
    copyTerm->setNextTerm(newTerm);

    cout << copyTerm->evaluate(3) << endl;

    cout << polyNomial->evaluate(3) << endl;


    return 0;
}

在这里,我期望 copyTerm 和 polyNomial 的评估是不同的,但它们是相同的

C++ 链表 复制构造函数 地址 取消引用

评论

2赞 Swift - Friday Pie 12/30/2020
确保构造函数进行初始化,例如Poly::Poly() : nextTerm (nullptr), trackCopy (nullptr)
0赞 Alan Birtles 12/30/2020
确保实现三法则。一个最小的可重复的例子会有所帮助
0赞 Ted Lyngmo 12/30/2020
"你们能帮我找到问题吗“——问题是如何表现出来的?
1赞 user4581301 12/30/2020
太复杂了。复制构造函数应该是 while more nodes in source list, copy current source node, add to copy to new list, advance to next node in source list.其他一切都只是其他虫子藏身的糠秕。
0赞 n. m. could be an AI 12/30/2020
假设您有一个长度为 2 的指针,因此是一个非 null 指针。你应该打多少次电话?你实际上打了多少次电话?PolynextTermnewnew

答:

3赞 Ted Lyngmo 12/30/2020 #1

您的复制构造函数有一个 bug。我添加了这个函数来显示链表和一个小程序来创建和复制它们:friendPoly

std::ostream& operator<<(std::ostream& os, const Poly& p) {
    const Poly* n = &p;
    os << '{';
    do {
        os << "addr:" << n << ' ' << n->power << ',' << n->coef << ' ';
        n = n->nextTerm;
    } while(n);
    return os << '}';
}

int main() {
    Poly p(1,2);
    p.setNextTerm(new Poly(3,4));

    std::cout << "orig: " << p << '\n'; // display the original
    Poly c(p);                          // copy construction
    std::cout << "copy: " << c << '\n'; // display the copy
}

请注意在副本中插入零的额外部分:Poly

orig: {addr:0x7ffcf2af8cd0 2,1 addr:0x1447eb0 4,3 }
copy: {addr:0x7ffcf2af8ce0 2,1 addr:0x1448ee0 0,0 addr:0x1448f00 4,3 }

如果像这样简化复制构造函数:

Poly::Poly(const Poly& rhs) : power(rhs.power), coef(rhs.coef), nextTerm(nullptr) {
    for(Poly *prev = this, *next = rhs.nextTerm; next; next = next->nextTerm) {
        prev->nextTerm = new Poly(next->coef, next->power);
        prev = prev->nextTerm;
    }
}

输出将如下所示:

orig: {addr:0x7ffdfe4b2eb0 2,1 addr:0x1f15eb0 4,3 }
copy: {addr:0x7ffdfe4b2ec0 2,1 addr:0x1f16ee0 4,3 }

评论

0赞 Ted Lyngmo 12/30/2020
@swittuh 不客气!我可能应该提到,我认为你的类也缺少析构函数:似乎缺少。~Poly() { delete nextTerm; }
1赞 swittuth 12/30/2020
是的,在我完成复制构造函数后,我要添加它,我卡在了哈哈上