I want to separate .h and .cpp for template class. Here is the what I was doing:
- I wrote directly .h and .cpp like without template. So it creates an exception like Link 2019 Template exception
- There are some solution to handle this How to define template class header and implement it in another cpp. I choose solution 3.
- According to the solution I added include *.cpp just before #endif inside header.(Still *.cpp includes *.h)(Below code represents this step) It gives
template has already been defined error.
- According to research the way of get rid of this error is(circular dependency) remove #include *.h from *.cpp but this time
unrecognizable template declaration/definition error
Occured. My question is if I include *.cpp to *.h file. How can we build project as expected? Or this solution is obsolute?
// TestTemp.h
#ifndef _TESTTEMP_H_
#define _TESTTEMP_H_
template<class T>
class TestTemp
{
public:
TestTemp();
void SetValue(T obj_i);
T Getalue();
private:
T m_Obj;
};
#include "TestTemp.cpp"
#endif
// TestTemp.cpp
#include "TestTemp.h"
template <class T>
TestTemp<T>::TestTemp()
{
}
template <class T>
void TestTemp<T>::SetValue(T obj_i)
{
}
template <class T>
T TestTemp<T>::Getalue()
{
return m_Obj;
}
#include "TestTemp.h"
int main()
{
TestTemp<int> a;
a.Getalue();
return 0;
}