I’m getting this error in my DynamicArray.cpp class: “error C2143: syntax error : missing ‘;’ before ‘template'” Third Line of DynamicArray.cpp.
It’s still a work in progress but I would also really aprecieate critizims on the design. Thank You
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)
}
You need a
;at the end of your class:Also, you need to have definitions for the templated functions in the header file. The way templates work the definition needs to be available to any code that calls the functions or accesses the data involved. The compiler creates a new instance of the function for each type of template used. This instantiation is done in the code that uses the templated data/functions.