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 {
	Vector<T>(); //default constructor                        
	explicit Vector( int num );  //alternative constructor
	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?

When templatizing a class, you need to use a template statement before the class definition. Additionally, a matching template statement should preceed each function implementation that is not part of the class's definition. You only need the Template Parameter on the class name, not the function name. This:

Vector<T>::Vector<T>() :capacity(0), size(0) {} //this is the line I am having problems with.

Should be this:

template <class T>
Vector<T>::Vector() :capacity(0), size(0) {} //this is the line I am having problems with.

You also need to put the function's return type and/or qualifiers (such as "explicit") before the class resolution, not after.

template <class T>
explicit Vector<T>::Vector( int num ) {
	//to be implemented.

An example:

//declare a templatized class
template <class T>
class TempClass {
   TempClass();      //default constructor
   T memberFunc(T);  //another member
};  //end class definition

//implement default constructor
template <class T>
TempClass<T>::TempClass() {}

//implement TempClass<T>::memberFunc()
template <class T>
T TempClass<T>::memberFunc(T someNewT) {
  T anotherNewT;

  /* do something */

  return anotherNewT;

Edited 6 Years Ago by Fbody: n/a

I THINK it works like this because new returns a pointer to the object. So I just declare a pointer to the object of that type and then give it the address new returns.

TempClass<int> *tv = new TempClass<int>;

hi ! :)
how can we instanciate the constructor in main() using "new" ?

You don't "instantiate the constructor" you instantiate the class. When you perform the instantiation, the appropriate constructor gets called as part of the instantiation process.

This article has been dead for over six months. Start a new discussion instead.