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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteHCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.