复制赋值运算符不起作用

Doesn't work Copy Assignment Operator

在我的代码中,我的复制赋值运算符有问题。当我尝试在 main() 中执行“=”运算符时,源数组 (numArr) 的内容未复制到目标数组 (numArr2)。

然后,对于我的 doubleCap() 函数,一旦我的原始数组已满,我将尝试创建一个双倍大小的更大数组。但是,如果我插入 delete[] newArr,编译器将在我的数组中输出一些随机数。

这是我在 .cpp 中的代码

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

using namespace std;

//Default Constructor
NumList::NumList()
{
    //Actual elements stored in the array
    size = 0;
    //Max capacity of the array
    capacity = 1;
    numList = new int[capacity];
}

//Destructor
NumList::~NumList()
{
    delete[] numList;
}

//Copy Constructor
NumList::NumList(const NumList& anotherNumList)
{
    capacity = anotherNumList.capacity;
    size = anotherNumList.size;
    numList = new int[capacity];
    for (int i = 0; i < size; ++i)
    {
        numList[i] = anotherNumList.numList[i];
    }
}

//Copy Assignment Operator
NumList& NumList::operator= (const NumList& anotherNumList)
{
    //Check if it is self-assigning
    if (this == &anotherNumList)
        return *this;

    //Get rid of the old data
    delete[] numList;

    this->capacity = anotherNumList.capacity;

    //Create and copy to the new array
    numList = new int[capacity];

    for (int i = 0; i < anotherNumList.size; ++i)
    {
        numList[i] = anotherNumList.numList[i];
    }

    return *this;
}

void NumList::print()
{
    for (int i = 0; i < size; ++i)
    {
        cout << numList[i] << " ";
    }
    cout << endl;
}

void NumList::doubleCap()
{
    capacity = capacity * 2;
    //Create a new array when the capacity is full
    int * newArr = new int[capacity];

    for (int i = 0; i < size; ++i)
    {
        newArr[i] = numList[i];

    }
    //Let numlist points to the new array
    numList = newArr;

    //delete[] newArr; <-- 
}

void NumList::insertEnd(int val)
{
    //Double the capacity of the list
    if (size == capacity)
    {
        doubleCap();
    }
    numList[size] = val;
    ++size;
}

void NumList::insertAt(int val, int index)
{
    if (index < 0 || index > capacity)
    {
        cout << "The index is out of range." << endl;
    }
    else
    {
        //Double the capacity of the list
        if (size == capacity)
        {
            doubleCap();
        }
        for (int i = (size-1); i >= index; i--)
        {
            numList[i + 1] = numList[i];
        }
        numList[index] = val;
        ++size;
    }
}

这是我在主程序中的代码

#include <iostream>
#include <string>
#include <algorithm>
#include "NumList.h"

using namespace std;

int main()
{
    NumList numArr;
    NumList numArr2;

    numArr.insertEnd(10);
    cout << "List 1 after inserting 10: ";
    numArr.print();

    numArr2 = numArr;
    NumList numArr3(numArr);

    numArr.insertEnd(11);
    numArr.insertEnd(12);
    numArr.insertAt(5, 0);
    cout << "List 1: ";
    numArr.print();

    cout << "\nPrint the list 2 of int: ";
    numArr2.print();

    cout << "\nPrint the list 3 of int: ";
    numArr3.print();

    system("pause");
    return 0;

}

无行输出"delete[] newArr;",

List 1 after inserting 10: 10 
List 1: 5 10 11 12

Print the list 2 of int: 

Print the list 2 of int: 10 
Press any key to continue . . . 

用行 "delete[] newArr;"、

输出
List 1 after inserting 10: 10 
List 1: 5 -572662307 -572662307 12

Print the list 2 of int: 

Print the list 2 of int: 10 
Press any key to continue . . . 

您正在尝试删除错误的内容。在 doubleCap() 中,您有 2 个数组,成员和您创建的新数组具有更多 space。多 space 的那个是您要保留的那个,所以您不能删除它。您需要做的是删除原始数组,然后将新数组分配给它。这使得函数看起来像

void NumList::doubleCap()
{
    capacity = capacity * 2;
    //Create a new array when the capacity is full
    int * newArr = new int[capacity];

    for (int i = 0; i < size; ++i)
    {
        newArr[i] = numList[i];

    }

    delete [] numList; // get rid of the old array
    //Let numlist points to the new array
    numList = newArr;
}

您还缺少 operator =anotherNumList.sizethis->size 的分配。这会导致复制的列表在赋值后大小错误。