s_lavie
asked on
Class Template - Linker Error
Hi,
I wrote a code in c++, using a template class.
When I put all function and declaration in one file (no project) it was just fine. But when I divided it to few files (using project) I had a linker error:
"undefined symbol a<int>::show_num() in module appl.cpp"
when "a" is the class template, and "show_num()" is a public function in it.
the same goes very well when I don't use the template.
Any idea ????????
please if can add an example.
Thank you.
I wrote a code in c++, using a template class.
When I put all function and declaration in one file (no project) it was just fine. But when I divided it to few files (using project) I had a linker error:
"undefined symbol a<int>::show_num() in module appl.cpp"
when "a" is the class template, and "show_num()" is a public function in it.
the same goes very well when I don't use the template.
Any idea ????????
please if can add an example.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points to 30
You can, but only if it will be used in only that .cpp file. If a template is used by multiple .cpp files, but the entire template in a .h file. If the template is useed on only one .cpp file, you can place it in the .cpp file (In that case, I would put everything for the template in the ,cpp file, you wouldn't want a .h for that.)