I am writing another header file. And I am still trying to get my head around these templates. They seem extremely easy to understand but I have had the compiler moaning at me for the last 2 hours.
This is what i have:
#include <iostream>
#include <cassert>
using namespace std;
template <class T=int> // default type int????
class Vector {
public:
Vector<T>(); //default constructor
explicit Vector( int num ); //alternative constructor
private:
int capacity;
int size;
T *array;
};
Vector<T>::Vector<T>() :capacity(0), size(0) {} //this is the line I am having problems with.
Vector::explicit Vector( int num ) {
//to be implemented.
//
}
I have tried moving the <T> to all sorts of places and even take it out. I am getting:
error: `T' was not declared in this scope
error: template argument 1 is invalid
and if I take it out completely I get this:
error: `template<class T> class Vector' used without template parameters
error: ISO C++ forbids declaration of `Vector' with no type
error: `int Vector()' redeclared as different kind of symbol
error: conflicts with function declaration `int Vector()'
error: only constructors take base initializers
This is supposed to be a constructor... I assume that if I create a new class using code:
new vector(); then it should be made of type int?? because I have used template <class T=int> ?
I have read this page on templates http://www.cplusplus.com/doc/tutorial/templates/ , but I still dont understand what I am doing wrong.
Could anyone help me out please?