C++ 不';t工作副本分配操作员
在我的代码中,我的复制分配运算符有问题。当我尝试在我的C++ 不';t工作副本分配操作员,c++,visual-c++,copy-assignment,C++,Visual C++,Copy Assignment,在我的代码中,我的复制分配运算符有问题。当我尝试在我的main()中执行“=”运算符时,我的源数组(numar)的内容不会复制到我的目标数组(numar2) 然后,对于我的doubleCap()函数,一旦原始数组已满,我将尝试创建一个更大的双倍大小的数组。但是,如果我插入delete[]newArr,编译器将在数组中输出一些随机数 这是我在.cpp中的代码 #include <iostream> #include "NumList.h" using namespace std;
main()
中执行“=”运算符时,我的源数组(numar
)的内容不会复制到我的目标数组(numar2
)
然后,对于我的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;
}
}
行为“delete[]newArr;”的输出
您试图删除错误的内容。在
doubleCap()
中,您有两个数组,您创建的成员和新成员具有更大的空间。有更多空间的是你想要保留的,所以你不能删除它。您需要做的是删除原始数组,然后将新数组分配给它。这使函数看起来像
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;
}
void NumList::doubleCap()
{
容量=容量*2;
//容量已满时创建新阵列
int*newArr=新int[容量];
对于(int i=0;i
您还缺少
操作符=
中另一个numlist.size
到此->size
的赋值。这会导致分配后复制的列表大小错误。您试图删除错误的内容。在doubleCap()
中,您有两个数组,您创建的成员和新成员具有更大的空间。有更多空间的是你想要保留的,所以你不能删除它。您需要做的是删除原始数组,然后将新数组分配给它。这使函数看起来像
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;
}
void NumList::doubleCap()
{
容量=容量*2;
//容量已满时创建新阵列
int*newArr=新int[容量];
对于(int i=0;i
您还缺少
操作符=
中另一个numlist.size
到此->size
的赋值。这会导致分配后复制的列表大小错误。您可能需要研究复制交换习惯用法。您可能需要研究复制交换习惯用法。此外,操作符=
@Slava中缺少对此->大小的分配。@NathanOliver只是澄清一下。在我的“operator=”中,当我已经声明了一个大小为“capacity”的新数组时,为什么需要分配“this->size”?在我的代码中,“大小”只是表示数组中存储的元素的大小。@code,因为print
使用size
,您需要正确的size
。在本例中,您默认构造了numArr2
,因此它的大小为0
,因此当您调用print时,您将打印0
元素。此外,操作符=
@Slava中缺少对此-/size
的赋值,我已将其添加到答案中。@NathanOliver只是澄清一下。在我的“operator=”中,当我已经声明了一个大小为“capacity”的新数组时,为什么需要分配“this->size”?在我的代码中,“大小”只是表示数组中存储的元素的大小。@code,因为print
使用size
,您需要正确的size
。在这种情况下,您默认构造了numArr2
,因此它的大小为0
,因此当您在其上调用print时,您将打印0
元素。
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 . . .
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;
}