0

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

然后,对于我的doubleCap()函数,一旦我的原始数组已满,我将尝试创建一个更大的双倍大小数组。但是,如果我 insert 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 . . . 
4

1 回答 1

4

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

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;
}

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

于 2018-05-24T21:18:46.913 回答