Dear All,
Let's say I have the following two functions in main.cpp.
template<class T>
T addition(T a, T b);
{
return a+b;
}
template<class TA, class TB>
Ta subtraction(TA a, TB b);
{
return a-b;
}
int main()
{
return 0;
}
I am using Dev C++. (Yes... I know... I will change to Code::Block later...) And I want to put the function header, function implementation and main.cpp in three separate files... How exactly should I do it? I know how to do separate compilation for programs without using templates. However, in this case with templates used, where should I put the two lines of template<class...>?
If that's the case, is it actually OK to for me to put all other function implementations in header files even if they are not templates? (Although I wouldn't do that, but is that theoretically OK?)
No, don't put implementations into headers(if you do so you may get a linker error: multiple definitions). Only template and inline functions belong there.
In the header you need to put the prototype/declaration of a function/class/struct. E.g.:
Hello coder777 and all,
I just tried it out, it seems that putting the template function as well as its implementation in the header file does not work (in Dev C++)! However, it is fine if I just place the whole declaration in a .cpp separated from main.cpp. Could there be something wrong here?
Regards,
Bosco