本课程适用于期中考试。它是一个像矢量一样工作的动态数组。问题是当一个新的索引被添加到数组中时,就会调用析构函数(当程序结束时)程序抛出异常并转到一个名为dgbheap.c的文件。当我只在析构函数中删除原点时会发生这种情况。
DynamicArray.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <new>
#include <cstdlib>
using namespace std;
template <class T>
class DynamicArray
{
private:
T *origin;
T *allocator;
int size;
int current;
public:
DynamicArray();
DynamicArray(int);
DynamicArray(const DynamicArray&);
~DynamicArray();
void add(int);
void empty();
void remove(int);
int getSize();
T &operator[](const int&);
const T operator=(const DynamicArray&);
void put(T&);
void put(T);
};
template <class T>
DynamicArray<T>::DynamicArray()
{
origin = new T[5];
allocator = NULL;
size = 5;
current = 0;
}
template <class T>
DynamicArray<T>::DynamicArray(int s)
{
size = s;
try
{
origin = new T[s];
allocator = NULL;
current = 0;
}
catch(bad_alloc)
{
cout << "Error: Bad memery allocation\n";
exit(EXIT_FAILURE);
}
}
template <class T>
DynamicArray<T>::DynamicArray(const DynamicArray& obj)
{
empty();
for(int counter = 0; counter < obj.size - 1; counter++)
{
origin[counter] = obj.origin[counter];
}
}
template <class T>
DynamicArray<T>::~DynamicArray()
{
delete [] origin;
delete [] allocator;
}
template <class T>
void DynamicArray<T>::add(int size)
{
allocator = new T[size];
for(int counter = 0; counter < this-> size - 1; counter++)
{
allocator[counter] = origin[counter];
}
delete [] origin;
origin = new T[size];
this->size = this->size + size;
for(int counter = 0; counter < this->size - 1; counter++)
{
origin[counter] = allocator[counter];
}
allocator = NULL;
}
template <class T>
void DynamicArray<T>::empty()
{
if(size >= 0)
delete [] origin;
}
template <class T>
int DynamicArray<T>::getSize()
{
return size;
}
template <class T>
T &DynamicArray<T>::operator[](const int &index)
{
return origin[index];
}
template <class T>
const T DynamicArray<T>::operator=(const DynamicArray &obj)
{
size = obj.size;
delete [] origin;
origin = new T[size];
for(int counter = 0; counter < size; counter++)
*(origin + counter) = *(obj.origin + counter);
return *origin;
}
template <class T>
void DynamicArray<T>::put(T &obj)
{
if(current == size - 1)
{
add(1);
origin[size - 1] = obj;
++current;
}
else
{
origin[current] = obj;
++current;
}
}
template <class T>
void DynamicArray<T>::put(T obj)
{
if(current > size - 1)
{
add(1);
origin[size - 1] = obj;
++current;
}
else
{
origin[current] = obj;
++current;
}
}
template <class T>
void DynamicArray<T>::remove(int location)
{
allocator = new T[size - 1];
int skipper = 0;
for(int counter = 0; counter < size - 1; counter++, skipper++)
{
if(counter != location)
allocator[counter] = origin[skipper]
else if(counter == location)
{
++skipper;
allocator[counter] = origin[skipper]
continue;
}
}
}
#endif // !DYNAMICARRAY_H
的main.cpp
#include <iostream>
#include <string>
#include "DynamicArray.h"
using namespace std;
int main()
{
DynamicArray<int> a(5);
a.put(21);
a.put(22);
a.put(30);
a.put(58);
a.put(87);
a.put(87);
cout << a[0] << endl;
cout << a[1] << endl;
cout << a[2] << endl;
cout << a.getSize() << endl;
system("pause");
return 0;
}
答案 0 :(得分:1)
您的副本构造函数未初始化origin
。它应该是这样的:
template <class T>
DynamicArray<T>::DynamicArray(const DynamicArray& obj)
: origin(new T[obj.size]),
size(obj.size),
allocator(NULL),
current(obj.current)
{
for(int counter = 0; counter < obj.size; counter++)
{
origin[counter] = obj.origin[counter];
}
}
另外请注意empty()
方法:
template <class T>
void DynamicArray<T>::empty()
{
delete [] origin;
origin = NULL;
size = 0;
}
答案 1 :(得分:1)
问题在于:
template void DynamicArray :: add(int size);
您混淆了具有相同名称的大小变量this-&gt; size和argument size
以下是更正的成员函数:
template <class T>
void DynamicArray<T>::add(int size)
{
allocator = new T[this->size]; // was just 'size', needs to be old size
for(int counter = 0; counter < this->size - 1; counter++)
{
allocator[counter] = origin[counter];
}
delete [] origin;
this->size = this->size + size; // was down one line
origin = new T[this->size]; // was 'size'. ie old size, not new size set in line above
for(int counter = 0; counter < this->size - 1; counter++)
{
origin[counter] = allocator[counter];
}
allocator = NULL;
}