【发布时间】:2017-04-10 20:04:03
【问题描述】:
我正在编写一个数组类模板,但析构函数有问题。
#ifndef ARRAY_CPP
#define ARRAY_CPP
using namespace std;
#include<iostream>
#include<sstream>
#include "Array.h"
//Define default constructor, initialize an array of size 10 with all elements being (0,0)
template<typename T> //Define type parameter
Array<T>::Array() {
size = 10;
m_data = new T[size];
}
template<typename T> //Define type parameter
Array<T>::Array(int si) {
size = si;
m_data = new T[size];
}
template<typename T> //Define type parameter
Array<T>::Array(const Array<T>& source) {
size = source.size; //Set the size equal to the size of the source
m_data = new T[size]; //Create a new source on the heap
//Loop through and copy each member of the source array to the new array
for (int i = 0; i < size; i++) {
m_data [i] = source.m_data [i];
}
}
//Define default destructor
template<typename T> //Define type parameter
Array<T>::~Array() {
delete[] m_data;
}
//Define operator =
template<typename T> //Define type parameter
Array<T>& Array<T>::operator = (const Array<T>& source) {
//Check self-assignment
if (this != &source) {
//Delete the old array
delete[] m_data;
size = source.size; //Set the size equal to the size of the source
m_data = source.m_data; //Set the dynamic C array equal to that of the source
//Loop through each element and copy each member of the source array to the current array
for (int i = 0; i < size; i++) {
m_data[i] = source.m_data [i];
}
}
//Return current array
return *this;
}
//Define operator[]
template<typename T> //Define type parameter
T& Array<T>::operator[](int index) {
if ((index < 0) || (index>= size))
throw -1;
return m_data[index];
}
//Define constant operator[]
template<typename T> //Define type parameter
const T& Array<T>::operator [] (int index) const {
if ((index < 0) || (index >= size))
throw -1;
return m_data[index];
}
using namespace std;
#include<iostream>
#include<sstream>
#include "Array.cpp"
void main() {
Array<double> test(10);
Array<double> test2(10);
test2 = test;
}
每当在 main 中调用数组的析构函数时,它都会给我错误:指定给 RtlValidateHeap 的地址无效。我知道这是因为我试图删除堆栈上的内存。但是,在我的构造函数中,我使用 new 初始化数组,这应该在堆上创建内存......在我实现模板之前代码运行良好。非常感谢!
【问题讨论】:
-
你能举一个完整的例子来说明这个问题吗?添加您需要添加的最简单的
main以演示问题,并显示完整的错误。您的类定义也不完整。试着让你的类完整并给出一个 main,并删除演示示例不需要的任何方法(例如,你可能不需要两个构造函数)。 -
1) 在析构函数中调用
delete[]时无需检查NULL。 2)您的赋值运算符存在很大缺陷。 3) 你的main函数在哪里? 4) 为什么在数组标题中包含Point.h? -
#ifndef ARRAY_CPP,你看why-can-templates-only-be-implemented-in-the-header-file了吗? -
我注意到您在头文件和源文件中拆分模板代码。 You might want to rethink that
-
m_data = source.m_data;应该是m_data = new T[size];(对于构造函数)。
标签: c++