Undefined reference, even though all object files are included

I have a template (non-member) function that is declared inside a namespace in an hpp file and defined in a cpp file. It is compiled, and the corresponding o file is sent to the linker OK. My main.cpp includes the relevant hpp file. But I still get an "undefined reference" error to the function when I try to call it from main(). I'm stumped. What could be wrong?
LVL 1
loveslaveAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SteHConnect With a Mentor Commented:
Templates need to be declared and defined in the hpp file. The compiler replaces the template arguments to generate actual code it will compile then. So the cpp won't be looked at, when you use the template inside another cpp file.
0
 
jkrCommented:
>>What could be wrong?

The problem is that - even though the C++ standard allows separating template declarations and implementations - there's no compiler out there that supports that. So, the easiest workaround is to

#include "template.cpp"
0
 
Sys_ProgCommented:
As of now, the most widely approach being declaring and defining the templates functions/classes in the header file itself since most compilers do not support separating template declarations from implementation.

Amit
0
 
Sys_ProgCommented:
The following link provides an explanatio for the same

http://www.cplusplus.com/doc/tutorial/tut5-1.html

Amit
0
All Courses

From novice to tech pro — start learning today.