I am trying to instantiate my sarray
class.
sarray array(10);
But I get an error saying that their is no constructor which has a matching definition.
The constructor is defined in the sarray header file
#ifndef SARRAY_H
#define SARRAY_H
template <class T>
class sarray {
public:
template<class T>sarray(int size);
~sarray();
template<class T> T& operator[](int i);
private:
int size;
T* data;
};
And is implemented in the sarray cpp file:
template<class T> sarray::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
Do I need to specify that I am using templates in the constructor call? The constructor argurments and the call arguments match so I am confused by the error message.
VS Syntax Error Message
C++ argument list for class template is missing
Full Source Code
#include "sarray.h"
#include <iostream>;
using namespace std;
template<class T> sarray::sarray(int size)
: size(size)
{
if (size > 0) data = new T[size];
else {
cout << "illegal array size = " << size << endl;
exit(1);
}
}
sarray::~sarray()
{
delete[] data;
}
template<class T> T& sarray::operator[](int i)
{
if (i < 0 || i >= size) {
cout << "index " << i << " is out of bounds." << endl;
exit(1);
}
return data[i];
}