I have written down a code to show myself how I can use templates. My class is in an .h file and my classdefinition is in an .cpp file.
Following content is in the .h file:
template <class T>
class one
{
private:
T data;
public:
one();
one(T s);
T two();
T getData();
};
Content of the .cpp file:
#include <iostream>
using namespace std;
#include "template.h"
template <class T>one<T>::one()
{
}
template <class T> one<T>::one(T s)
{
}
template <class T> T one<T>::two()
{
return 0;
}
template <class T> T one<T>::getData()
{
return 0;
}
with the following file I try to indicate the object of the class one:
int main()
{
one<string> * s = new one<string>;
return 0;
}
Now I got following compiler error:
/tmp/cchK1XpI.o: In function main':
templatetest.cpp:(.text+0x1c): undefined reference to
one::one()'
collect2: error: ld returned 1 exit status
I tried to understand where the wrong reference is but cant find it. (I tried it aswell with type int and the same error showed. So if anyone have a solution for this please share it with me!