Ok so I understand how function templates work. I am having issues trying to make a class template. I have three separate files, all in the same project, I am using Visual Studios 2010.There is a header file, an implementation file, and the main file. Here is what I have so far, but I get an error message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
//Header File
#ifndef RECTANGLE_H
#define RECTANGLE_H
template <class T>
class Rectangle
{
private:
//T width;
//T length;
public:
Rectangle();
/*
Rectangle(T, T);
void setLength(T);
void setWidth(T);
//template <class T>
T getLength() const
{ return length; }
//template <class T>
T getWidth() const
{ return width; }
*/
};
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
//implementation file
#include"Rectangle.h"
#include<iostream>
using namespace std;
template<class T>
Rectangle<T>::Rectangle()
{
cout<<"Hello";
}
/*template<class T>
Rectangle<T>::Rectangle(T l, T w)
{
length = l;
width = w;
}
template<class T>
void Rectangle<T>::setLength(T l)
{
length = l;
}
template<class T>
Rectangle<T>::setWidth(T w)
{
width = w;
}
*/
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//Testing Rectangle.h
#include "Rectangle.h"
#include <iostream>
using namespace std;
int main()
{
Rectangle<int> r1;
//r1.setLength(5);
//cout<<"Length: ";
//r1.getLength();
return 0;
}
|
I get the following errors:
error C2995: 'Rectangle<T>::Rectangle(void)' : function template has already been defined
: see declaration of 'Rectangle<T>::Rectangle'
As you can see I cannot even create an instance of the Rectangle<T> class
On a side note, I completely commented out the .cpp file. Then in the header file I assigned width and length 0 in the default constructor. I uncommented the getLength and getWidth functions, but when I called them they did not return anything. So this leads me to believe that when using class templates, you cannot have a .cpp file. Is this correct?