【发布时间】:2019-09-14 04:33:18
【问题描述】:
我正在做家庭作业。我正在尝试为我正在创建的 Array 类重载“=”运算符,以便它将为新创建的数组分配与另一个数组相同的值。这似乎有效。创建数组并复制数据。我还检查了数组第一个元素的位置,它与原始元素不同,所以我不认为它试图删除已删除的数组。
我试过搞乱我的析构函数,但老实说我不知道这是从哪里来的。如果有人有任何可能有帮助的调试策略,我也很想听听。
驱动程序.cpp
int main ()
{
//Initialize
int size = 0;
char fill = '\0';
//Get info about the array
std::cout << "How long should the array be?" << std::endl;
std::cin >> size;
std::cout << "Choose fill character." << std::endl;
std::cin >> fill;
//Create array & Print array details
Array* arr = new Array(size, fill);
std::cout << "The array size is: " << arr->size() << std::endl;
std::cout << "max size: " << arr->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr->printArr();
std::cout << std::endl;
//Create new array & set it's values equal to old array
Array* arr2 = new Array();
arr2 = arr;
//= OVERLOAD TESTING
std::cout << "The array size is: " << arr2->size() << std::endl;
std::cout << "max size: " << arr2->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr2->printArr();
//Deallocate memory
delete arr;
arr = nullptr;
delete arr2;
arr2 = nullptr;
//Checking for memory leaks
_CrtDumpMemoryLeaks();
return 0;
}
Array.cpp 文件
//Define MAX SIZE so that it can be easily changed.
#define MAX_SIZE_ 200
#include "Array.h"
#include <iostream>
#include <stdexcept>
Array::Array (void)
:data_ (new char[MAX_SIZE_]),
cur_size_ (0),
max_size_ (MAX_SIZE_)
{ }
//Overloaded Constructor
//Assigns the initial size of the array and fills each element with the character stored in fill.
Array::Array (size_t length, char fill)
: data_ (new char[length]),
cur_size_ (length),
max_size_ (length)
{
//Fill each element with the character passed in to the function.
for(int i = 0; i < length; i++)
{
this-> data_[i] = fill;
}
std::cout << &this->data_ << std::endl;
}
//Destructor
Array::~Array (void)
{
delete[] this->data_;
this->data_ = nullptr;
}
//Sets new array equal to rhs.
const Array & Array::operator = (const Array & rhs)
{
//Set current and max size values to new array.
this->max_size_ = rhs.max_size_;
this->cur_size_ = rhs.cur_size_;
//Copy data from rhs.data_ to new array's data_
for(int i = 0; i < rhs.cur_size_; i++)
{
this->data_[i] = rhs.data_[i];
}
return *this;
}
//Print the contents of the array.
void Array::printArr(void)
{
for (int i = 0; i < (this->cur_size_) ; i++)
{
std::cout << this->data_[i];
}
}
Expected Results: The program displays information about the different arrays, then deletes them with no memory leaks.
Actual Results: The program displays all the correct data for both arrays and is able to delete the first array without a hitch, but runs into an exception when calling:
delete[] this->data_;
on the second array.
> Exception thrown at 0x5D13DB1B (ucrtbased.dll) in driver.exe: 0xC0000005: Access violation reading location 0xDDDDDDCD
Thanks for any help!
【问题讨论】:
-
Array的复制构造函数在哪里?换句话说,Array(const Array& )在哪里?你需要它,否则你就没有完全正确地实施规则 3。 -
另外,为什么在
main中你这样做:Array* arr = new Array(size, fill);而不是Array arr(size, fill);?似乎您对代码应该如何工作的想法没有被完全理解。另外,这条评论不是真的://COPY CONSTRUCTOR TESTING-- 代码中没有使用复制构造函数。 -
另外你没有包含 Array.h 可能有一些缺失的部分
-
Array* arr2 = new Array(); arr2 = arr;
-
对@JeremyFriesner 提到的内容是的。如果您不使用指针,如我在第二条评论中提到的那样,实现了复制构造函数,那么您将取得一些进展。现在你完全搞砸了
main错误的指针使用,你有一个不完整的Array类,希望它能正常工作。
标签: c++ visual-studio debugging