我在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
DynamicArray.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)
}
答案 0 :(得分:1)
课程结束时需要;
:
T operator=(const T&);
void insert(T&);
};
#endif // !DYNAMICARRAY_H
此外,您需要在头文件中定义模板化函数。模板工作定义的方式需要可用于调用函数或访问所涉及数据的任何代码。编译器为使用的每种类型的模板创建函数的新实例。此实例化在使用模板化数据/函数的代码中完成。
答案 1 :(得分:1)
有两个问题。首先,在类声明结束时需要;
。修复后,您将遇到问题,因为模板方法定义也应该在头文件中,或者在尝试实例化模板的代码可以访问的文件中。