Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制构造函数和赋值运算符问题_C++_Linked List_Copy Constructor_Assignment Operator - Fatal编程技术网

C++ 复制构造函数和赋值运算符问题

C++ 复制构造函数和赋值运算符问题,c++,linked-list,copy-constructor,assignment-operator,C++,Linked List,Copy Constructor,Assignment Operator,我正在为家庭作业创建一个二进制链表,它只存储1位的度数。我可以获得最高阶数,在二进制列表中的任何位置设置位,并返回在特定阶数出现的位,但由于某些原因,我在创建复制构造函数和赋值(=)运算符时遇到了最大的困难。以下是我的代码: // copy constructor // creates a new linked list where the contents are a deep copy of the provided list Binary::Binary(const Binary &

我正在为家庭作业创建一个二进制链表,它只存储1位的度数。我可以获得最高阶数,在二进制列表中的任何位置设置位,并返回在特定阶数出现的位,但由于某些原因,我在创建复制构造函数和赋值(=)运算符时遇到了最大的困难。以下是我的代码:

// copy constructor
// creates a new linked list where the contents are a deep copy of the provided list
Binary::Binary(const Binary &b)
{
    Binary clone;
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    {
        clone.set_bit(1, current_other->degree); 
    }
}

// assignment operator
// sets the current link list to be a deep copy of the provided list.
// make sure to check if assigning to itself, and make sure to free old memory
// before making the copy.
Binary& Binary::operator=(const Binary &other)
{
    Binary clone;
    for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
    {
        clone.set_bit(1, current_other->degree); 
    }
    return clone;
}
我的逻辑有问题吗?有人请帮忙

另外,我已经测试了我的set_位(b,d)和其他方法很多次了,我知道这些是唯一出错的方法,因为当我尝试“Binary b3(b2)”或“Binary b3=b2”时,程序在该点停止,并说“赋值1.exe中0x00DC4B18处未处理的异常:0xC0000005:访问冲突读取位置0xCCD0”


编辑:我还有一个默认构造函数: Binary(){firstTerm=nullptr;}

编辑:

输出:

    TESTING DEFAULT CONSTRUCTOR
    The binary number b1 is empty.

    TESTING GET AND SET METHODS
    The highest bit of binary number b1 is 5.
    The bit of binary number b1 at degree 5 is 1.
    The bit of binary number b1 at degree 2 is 0.
    The bit of binary number b1 at degree 1 is 0.

    TESTING PARAMETER CONSTRUCTOR
    The bit of binary number b1 at degree 2 is 1.
    The bit of binary number b1 at degree 0 is 1.
    The bit of binary number b1 at degree 1 is 0.

    TESTING COPY CONSTRUCTOR
    B2 = 101
    B3 = _

Unhandled exception at 0x00C04B18 in Assignment 1.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.
测试仪代码:

#include <iostream>
#include "binary.h"

using namespace std;

int main (void)
{
    // test default constructor
    Binary b1;
    cout << "TESTING DEFAULT CONSTRUCTOR" << endl;
    if (b1.get_degree() == -1)
        cout << "\tThe binary number b1 is empty." << endl;
    else
        cout << "\tThe binary number b1 is NOT empty. (INCORRECT)" << endl;

    // test get_bit, set_bit, and get_degree
    cout << "\nTESTING GET AND SET METHODS" << endl;
    b1.set_bit(1, 2);
    b1.set_bit(1, 5);
    b1.set_bit(1, 0);
    b1.set_bit(0, 2);
    if (b1.get_degree() == 5)
        cout << "\tThe highest bit of binary number b1 is 5." << endl;
    else
        cout << "\tThe highest bit of binary number b1 is NOT 5. (INCORRECT)" << endl;
    if (b1.get_bit(5) == 1)
        cout << "\tThe bit of binary number b1 at degree 5 is 1." << endl;
    else
        cout << "\tThe bit of binary number b1 at degree 5 is 0. (INCORRECT)" << endl;
    if (b1.get_bit(2) == 0)
        cout << "\tThe bit of binary number b1 at degree 2 is 0." << endl;
    else
        cout << "\tThe bit of binary number b1 at degree 2 is 1. (INCORRECT)" << endl;
    if (b1.get_bit(1) == 0)
        cout << "\tThe bit of binary number b1 at degree 1 is 0." << endl;
    else
        cout << "\tThe bit of binary number b1 at degree 1 is 1. (INCORRECT)" << endl;

    // test parameter constructor
    cout << "\nTESTING PARAMETER CONSTRUCTOR" << endl;
    Binary b2(5);

    if (b2.get_bit(2) == 1)
        cout << "\tThe bit of binary number b2 at degree 2 is 1." << endl;
    else
        cout << "\tThe bit of binary number b2 at degree 2 is 0. (INCORRECT)" << endl;
    if (b2.get_bit(0) == 1)
        cout << "\tThe bit of binary number b2 at degree 0 is 1." << endl;
    else
        cout << "\tThe bit of binary number b2 at degree 0 is 0. (INCORRECT)" << endl;
    if (b2.get_bit(1) == 0)
        cout << "\tThe bit of binary number b2 at degree 1 is 0." << endl;
    else
        cout << "\tThe bit of binary number b2 at degree 1 is 1. (INCORRECT)" << endl;

    // test copy constructor
    cout << "\nTESTING COPY CONSTRUCTOR" << endl;
    cout << "B2= " << b2 << endl;
    b2.set_bit(1,1);
    Binary b3(b2);
    cout << "B3= " << b3 << endl;
    b2.set_bit(1, 1);
    cout << "B2= " << b2 << endl;
    cout << "B3= " << b3 << endl;
    if (b3.get_bit(2) == 1)
        cout << "\tThe bit of binary number b3 at degree 2 is 1." << endl;
    else
        cout << "\tThe bit of binary number b3 at degree 2 is 0. (INCORRECT)" << endl;
    if (b3.get_bit(0) == 1)
        cout << "\tThe bit of binary number b3 at degree 0 is 1." << endl;
    else
        cout << "\tThe bit of binary number b3 at degree 0 is 0. (INCORRECT)" << endl;
    if (b3.get_bit(1) == 0)
        cout << "\tThe bit of binary number b3 at degree 1 is 0." << endl;
    else
        cout << "\tThe bit of binary number b3 at degree 1 is 1. (INCORRECT)" << endl;

    // test assignment operator
    cout << "\nTESTING ASSIGNMENT OPERATOR" << endl;
    b2 = b3;
    b3.set_bit(1, 1);

    if (b2.get_bit(2) == 1)
        cout << "\tThe bit of binary number b2 at degree 2 is 1." << endl;
    else
        cout << "\tThe bit of binary number b2 at degree 2 is 0. (INCORRECT)" << endl;
    if (b2.get_bit(0) == 1)
        cout << "\tThe bit of binary number b2 at degree 0 is 1." << endl;
    else
        cout << "\tThe bit of binary number b2 at degree 0 is 0. (INCORRECT)" << endl;
    if (b2.get_bit(1) == 0)
        cout << "\tThe bit of binary number b2 at degree 1 is 0." << endl;
    else
        cout << "\tThe bit of binary number b2 at degree 1 is 1. (INCORRECT)" << endl;

    // test convert
    cout << "\nTESTING CONVERT METHOD" << endl;
    if (b1.convert() == 33)
        cout << "\tThe decimal value of binary number b1 is 33." << endl;
    else
        cout << "\tThe decimal value of binary number b1 is NOT 33. (INCORRECT)" << endl;

    // test output operator
    cout << "\nTESTING OUTPUT OPERATOR" << endl;
    cout << "\tThe binary number b1 is " << b1 << endl;
    cout << "\tThe number b1 should be 100001" << endl;

    // test addition
    cout << "\nTESTING ADDITION OPERATOR" << endl;
    Binary b4 = b2 + b3;

    if (b4.convert() == 12)
        cout << "\t101 + 111 = 1100." << endl;
    else
        cout << "\t101 + 111 != 1100. (INCORRECT)" << endl;

    // test subtraction
    cout << "\nTESTING SUBTRACTION OPERATOR" << endl;
    Binary b5(b1 - b2);

    if (b5.convert() == 28)
        cout << "\t100001 - 101 = 11100." << endl;
    else
        cout << "\t100001 - 101 != 11100. (INCORRECT)" << endl;

    // test multiplication
    cout << "\nTESTING MULTIPLICATION OPERATOR" << endl;
    Binary b6 = b3 * b2;

    if (b6.convert() == 35)
        cout << "\t111 * 101 = 100011." << endl;
    else
        cout << "\t111 * 101 != 100011. (INCORRECT)" << endl;

    system("pause");
}
#包括
#包括“binary.h”
使用名称空间std;
内部主(空)
{
//测试默认构造函数
二元b1;
库特
您创建了一个名为“克隆”的对象,设置其位,然后将其丢弃。这似乎不正确。您的意思可能是:

Binary::Binary(const Binary &b)
{
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    {
        set_bit(1, current_other->degree); 
    }
}
您创建了一个名为“克隆”的对象,设置其位,然后将其丢弃。这似乎不正确。您的意思可能是:

Binary::Binary(const Binary &b)
{
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    {
        set_bit(1, current_other->degree); 
    }
}

A一旦定义了构造函数,编译器就不会为您生成默认构造函数,这意味着您不能进行
Binary克隆;
如果您没有定义
Binary::Binary()
constructor.

A一旦定义了构造函数,编译器就不会为您生成默认构造函数,这意味着您不能进行
二进制克隆;
如果您没有定义
二进制::二进制()
constructor.

我找到了答案,谢谢大家的帮助。我找到了我的问题。正如有人指出的,在复制构造函数中,我相信我还没有像在其他构造函数中那样声明firstTerm。最后的代码是:

Binary::Binary(const Binary &b)
{
    firstTerm = nullptr; //construct firstTerm
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    //set a node pointer = to b's firstTerm then go through b's list, setting each bit and degree to the new list
    {
        set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
    }   
}
Binary &Binary::operator=(const Binary &other)
{
    if(this != &other) //make sure it isn't copying to itself
    {
        if (this->get_degree() != -1) //if the Binary list isn't empty, destruct it
        {
            this->~Binary();
        }
        firstTerm = nullptr; //construct firstTerm
        for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
        //set a node pointer = to other's firstTerm then go through other's list, setting each bit and degree to the new list
        {
            set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
        }   

        return *this;
    }
}
在赋值运算符中,如果需要的话,我没有首先破坏当前列表,也没有确保它没有复制到自身。此外,我将&放在错误的位置。最后的代码是:

Binary::Binary(const Binary &b)
{
    firstTerm = nullptr; //construct firstTerm
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    //set a node pointer = to b's firstTerm then go through b's list, setting each bit and degree to the new list
    {
        set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
    }   
}
Binary &Binary::operator=(const Binary &other)
{
    if(this != &other) //make sure it isn't copying to itself
    {
        if (this->get_degree() != -1) //if the Binary list isn't empty, destruct it
        {
            this->~Binary();
        }
        firstTerm = nullptr; //construct firstTerm
        for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
        //set a node pointer = to other's firstTerm then go through other's list, setting each bit and degree to the new list
        {
            set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
        }   

        return *this;
    }
}

我明白了,谢谢大家的帮助。我明白了我的问题。在复制构造函数中,正如有人指出的,我相信我还没有像在其他构造函数中那样声明firstTerm。最终的代码是:

Binary::Binary(const Binary &b)
{
    firstTerm = nullptr; //construct firstTerm
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    //set a node pointer = to b's firstTerm then go through b's list, setting each bit and degree to the new list
    {
        set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
    }   
}
Binary &Binary::operator=(const Binary &other)
{
    if(this != &other) //make sure it isn't copying to itself
    {
        if (this->get_degree() != -1) //if the Binary list isn't empty, destruct it
        {
            this->~Binary();
        }
        firstTerm = nullptr; //construct firstTerm
        for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
        //set a node pointer = to other's firstTerm then go through other's list, setting each bit and degree to the new list
        {
            set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
        }   

        return *this;
    }
}
在赋值运算符中,如果需要的话,我没有首先破坏当前列表,也没有确保它没有复制到自身。此外,我将&放在错误的位置。最后的代码是:

Binary::Binary(const Binary &b)
{
    firstTerm = nullptr; //construct firstTerm
    for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next) 
    //set a node pointer = to b's firstTerm then go through b's list, setting each bit and degree to the new list
    {
        set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
    }   
}
Binary &Binary::operator=(const Binary &other)
{
    if(this != &other) //make sure it isn't copying to itself
    {
        if (this->get_degree() != -1) //if the Binary list isn't empty, destruct it
        {
            this->~Binary();
        }
        firstTerm = nullptr; //construct firstTerm
        for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next) 
        //set a node pointer = to other's firstTerm then go through other's list, setting each bit and degree to the new list
        {
            set_bit(1, current_other->degree); //only 1 bits exist in this list, so you'll only set one bits at each degree
        }   

        return *this;
    }
}

我相信for operator=使用this指针,而不是创建新的二进制文件并克隆它。您是否定义了默认构造函数?(不带任何参数的构造函数)我们需要你的完整类来帮助你…@Roberto我发布了它。我相信for operator=使用this指针,而不是创建新的二进制并克隆它。你定义了默认构造函数吗?(一个不带任何参数的构造函数)我们需要你的完整类来帮助你…@Roberto我发布了它。我还有一个默认构造函数:Binary(){firstTerm=nullptr;}我也有一个默认构造函数:Binary(){firstTerm=nullptr;}“它不工作”不是一个有用的问题描述。它编译了吗?它运行了吗?你期望得到什么结果?你得到了什么?(您在
操作符=
方面也有类似的问题——它不会修改调用它的对象,而这正是这些操作符通常所做的。)很抱歉,我当时正赶着去上班。它给我的错误与我用另一种方式得到它时的错误相同。程序可以编译,但当我试图对复制的二进制列表执行任何操作时,它出现了我在原始帖子中发布的“访问冲突读取”错误。例如,在main()中,我有:二进制b3(b2);无法发布足够的代码让我们复制问题或使用调试器进行调试。我已发布了全部代码。非常感谢您迄今为止的帮助。“它不工作”不是一个有用的问题描述。它是否编译?是否运行?您期望得到什么结果?您得到了什么?(您在
操作符=
方面也有类似的问题——它不会修改调用它的对象,而这正是这些操作符通常所做的。)很抱歉,我当时正赶着去上班。它给我的错误与我用另一种方式得到它时的错误相同。程序可以编译,但当我试图对复制的二进制列表执行任何操作时,它出现了我在原始帖子中发布的“访问冲突读取”错误。例如,在main()中,我有:二进制b3(b2);无法发布足够的代码来复制问题或使用调试器进行调试。我已发布了全部代码。非常感谢您迄今为止的帮助。