Benjamin Johnson Benjamin Johnson - 9 months ago 66
C++ Question

Invalid use of template-name 'SmartArray' without an argument list

So the title states the problem I'm running into with my class constructors/destructor. Here's my code:

template <class ArrType>
class SmartArray{
ArrType *elements; // pointer that will point to dynamic array
int length(); // function to return array length
SmartArray<ArrType>(); // default constructor
SmartArray(int arrSize); // constructor that initializes array size
~SmartArray(); // destructor
void resizeArr(int newsize); // function that resizes array
SmartArray(const SmartArray& otherObject); // copy constructor

And here's the default constructor:

arrSize = 0;
elements = new ArrType[arrSize];
cout << "Created array using default constructor." << endl; // letting user know that object was successfully created

I did try looking the problem up but either those programs were too advanced for me to understand or I was just too stupid. In any case, I'm hoping there's a simple fix for this.

Answer Source

You have to use the template keyword to define the constructor, also:

template<class ArrayType>

   // ...

P.S. if you can confirm that you have this constructor definition placed in a separate .cpp file, your question will, unfortunately, also have to be closed as a duplicate of this question.