My 'template' does not compile

I have
.h file
1
2
3
4
template <class T>
std::string formato(T number, string format);
template <class V>
QString Qformato(V number, string format);

and at cpp:
1
2
3
4
5
6
7
8
template <class T>
std::string formato(T number, string format)  {
  return "....";
}
template <class V>
QString Qformato(V number, string format)  {
  return QString::fromStdString(formato(number,format));
}


And i have :
undefined reference to `QString Qformato<int>(int, std::string)'

What I'm doing bad? Maybe I cannot have templates with h and cpp??
Thanks

Last edited on
Do you include the header file in the cpp file? Do you compile the cpp file (not the h file)? Is formato also a template (it won't work if it's not a template or overloaded for the possible arguments).
What I'm doing bad? Maybe I cannot have templates with h and cpp??

Basically no, you cannot have template definitions in source (.cpp) files. This is in accordance with The Inclusion Model1. Move the template function definitions into the header file and give that a try.


1 I don't want to get into the different models at this time; just know that there are other (less popular) ways to deal with templates.
Last edited on
Topic archived. No new replies allowed.