不起作用复制分配操作员

时间:2018-05-24 21:14:09

标签: c++ visual-c++ copy-assignment

在我的代码中,我的复制赋值运算符出现问题。当我尝试在main()中执行“=”运算符时,我的源数组(numArr)的内容不会复制到我的destenation数组(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 . . . 

1 个答案:

答案 0 :(得分: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.sizethis->sizeoperator =的分配。这导致复制的列表在分配后具有错误的大小。