I'm making a basic array class in VS2015. It initially worked when the function code was in the header file, but now that I've moved that over to a .cpp file I get a C1004: unexpected end-of-file found for the header file.
Array.h:
#pragma once
template <class T> class Array
{
private:
T *m_data;
int m_length;
public:
Array() : m_length(0), m_data(nullptr) {}
Array(int length) : m_length(length);
Array(Array<T>& copyArray) : Array(copyArray.size());
~Array();
void clear();
T& at(int index);
T& operator[](int index);
int size();
};
Array.cpp:
#include "Array.h"
template<typename T>
Array::Array(int length) : m_length(length)
{
m_data = new T[m_length];
}
template<typename T>
Array::Array(Array<T>& copyArray) : Array(copyArray.size())
{
for (int index = 0; index < copyArray.size(); ++index)
{
at(index) = copyArray[index];
}
}
template<typename T>
Array::~Array()
{
delete[] m_data;
m_data = nullptr;
}
template<typename T>
void Array::clear()
{
delete[] m_data;
m_data = nullptr;
m_length = 0;
}
template<typename T>
T& Array::at(int index)
{
if (m_data != nullptr)
{
if (index < m_length && index >= 0)
{
return m_data[index];
}
throw OutOfBoundsException;
}
throw NullArrayException;
}
template<typename T>
T& Array::operator[](int index)
{
if (m_data != nullptr)
{
if (index < m_length)
{
return m_data[index];
}
else
throw "Out of array bounds";
}
else
throw "Array is null";
}
template<typename T>
int Array::size()
{
return m_length;
}