我的 DynamicArray.cpp 类中出现此错误:“错误 C2143:语法错误:缺少 ';' 在“模板”之前”DynamicArray.cpp 的第三行。这仍然是一项正在进行的工作,但我也非常感谢对设计的批评。谢谢 DynamicArray.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
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 erase(int);
void empty();
T at(int);
void put(T);
void remove();
DynamicArray operator=(const DynamicArray&);
int getSize();
void operator[](int);
T operator=(const T&);
void insert(T&);
}
#endif // !DYNAMICARRAY_H
动态数组.cpp
#include "DynamicArray.h"
template <class T>
DynamicArray<T>::DynamicArray()
{
origin = new T[5];
allocator = NULL;
size = 5;
current = 0;
for(int counter = 0; counter <= size - 1; counter++)
{
*origin[counter] = NULL;
}
}
template <class T>
DynamicArray<T>::DynamicArray(int size)
{
this->size = size;
origin = new T[size];
for(int counter = 0; counter <= size - 1; counter++)
{
*origin[counter] = NULL;
}
}
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;
size = NULL;
}
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];
}
origin = NULL;
origin = new T[size];
this->size = size;
for(int counter = 0; counter < size - 1; counter++)
{
*origin[counter] = allocator[counter];
}
allocator = NULL;
}
template <class T>
void DynamicArray<T>::erase(int ammount)
{
if(ammount - size > size)
throw "\nnegetive memory location error\n";
allocator = new T[size - ammount];
for(int counter = 0; counter < this-> size - ammount - 1; counter++)
{
*allocator[counter] = origin[counter];
}
origin = NULL;
size = size - ammount;
origin = new T[size];
for(int counter = 0; counter < size - 1; counter++)
{
origin[counter] = allocator[counter];
}
allocator = NULL;
}
template <class T>
void DynamicArray<T>::empty()
{
origin = NULL;
allocator = NULL;
}
template <class T>
T DynamicArray<T>::at(int location)
{
if(location > size || location < 0)
{
throw "\nMemory Location does not exist\n";
}
else
{
current = location;
return *origin[location];
}
}
/*template <class T>
void DynamicArray::put(T item)
{
add(1);
origin[size - 1] = item;
}*/
template <class T>
void DynamicArray<T>::remove()
{
erase(1);
}
template <class T>
DynamicArray<T> DynamicArray<T>::operator=(const DynamicArray& obj)
{
empty();
for(int counter = 0; counter < obj.size - 1; counter++)
{
origin[counter] = obj.origin[counter];
}
return *this;
}
template <class T>
int DynamicArray<T>::getSize()
{
return size;
}
template <class T>
void DynamicArray<T>::operator[](int index)
{
at(index);
}
template <class T>
T DynamicArray<T>::operator=(const T &value)
{
put(value);
return *this;
}
template <class T>
void DynamicArray<T>::insert(T &value)
{
int counter = 0;
do
{
if(*origin[counter] == NULL)
{
origin[counter] = value;
}
counter++
}
while(*origin[counter] != NULL)
}