My aim to create a very simple class template. I can do it in one .cpp file, but I insist on having the same class defined in .h file, its method in .cpp file and finally execute it in .cpp (main) and have some troubles.
So, in class.h
I have what follows:
#include<iostream>
using namespace std;
template <class T>
class templateClass {
T x, y;
public:
templateClass(T, T); // constructor
T bigger();
};
On the hand in methods.cpp
there is:
#include<iostream>
#include"class.h"
using namespace std;
template <class T>
templateClass <T> ::templateClass(T a, T b) {
x = a;
y = b;
}
template <class T>
T templateClass<T>::bigger() {
return (x > y ? x : y);
}
Finally in main.cpp
I have
#include<iostream>
#include"class.h"
using namespace std;
int main()
{
templateClass<int> obj(12, 47);
return 0;
}
In the end, I obtain an error unresolved external symbol "public: __thiscall templateClass::templateClass(int,int)" (??0?$templateClass@H@@QAE@HH@Z) referenced in function _main templateClass ...
Can you help me with explaing what is wrong and why the same works if put everything in one .cpp file without seperating?