I am trying to write a template class that can form classes depending on the <class>
I pass. The problem is I cannot declare and define in the same .h
file. In my project, the UTF tool can only work with the .cpp
files (for code coverage, etc.). I have seen in a blog that they say “Add .cpp
instead of .h
”. Is this advisable?
Template.h
#ifndef TEMPLATE_H_
#define TEMPLATE_H_
template<class T>
class Template
{
public:
T Add(T a,T b);
};
#endif /* TEMPLATE_H_ */
Template.cpp
#include "Template.h"
template<class T>
T Template<T>::Add(T a, T b)
{
return a+b;
}
Main.cpp
#include "Template.cpp" //Is this a good practise?
#include <iostream>
int main(int argc, char **argv) {
Template<int> obj;
std::cout<<obj.Add(3,4)<<std::endl;
}
If this is not advisable then how do I solve this issue? export
?