469,909 Members | 1,985 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

linker error when using template class in Dev-C++

I have designed a package with several files, which is listed bellow

base.cpp & base.hpp - define the template class base
tree.cpp & tree.hpp - define the class tree derived from base<int>
client.cpp & client.hpp - define the class client derived from tree
main.cpp - the main function instantiate client and run

All these files are compiled successfully, but there's a linker error " [Linker error] undefined reference to `base<int>::base_run()' ".

However, I have defined this function in base.cpp, and all .cpp files are added to the project. I use IDE Dev-C++ 4.9.9.2. I don't know why the linker cannot find this function. When I get ride of the template, then problem solved. But I really need the class "base" to be a template class. When I use the template but melt all these files into one file, problem also solved. But I don't like that.

I have made the program to be very small (doing nothing, only shows the problems) and attached it with this note in the file problem_example.zip.

Can anybody tell me why this happen? what's wrong with template class?

thanks a lot
Attached Files
File Type: zip problem_example.zip (6.7 KB, 355 views)
Sep 27 '05 #1
2 9021
I have designed a package with several files, which is listed bellow

base.cpp & base.hpp - define the template class base
tree.cpp & tree.hpp - define the class tree derived from base<int>
client.cpp & client.hpp - define the class client derived from tree
main.cpp - the main function instantiate client and run

All these files are compiled successfully, but there's a linker error " [Linker error] undefined reference to `base<int>::base_run()' ".

However, I have defined this function in base.cpp, and all .cpp files are added to the project. I use IDE Dev-C++ 4.9.9.2. I don't know why the linker cannot find this function. When I get ride of the template, then problem solved. But I really need the class "base" to be a template class. When I use the template but melt all these files into one file, problem also solved. But I don't like that.

I have made the program to be very small (doing nothing, only shows the problems) and attached it with this note in the file problem_example.zip.

Can anybody tell me why this happen? what's wrong with template class?

thanks a lot

I too have a very similar problem at the moment and I too am using the Dev C++ IDE as well, while searching the web for a solution, I found this FAQ about this problem. Read 35.12 through 35.15.

haplotype I hope that link helps you.

Although it did solve the problem, I don't really like the solutions it gives (not to mention the first & third (export keyword) solutions don't even work with the Dev C++ Complier). So my question is, is there another solution to this problem with templates or am I just gonna have to use the solutions the FAQ gave?
Dec 25 '07 #2
As of now, compilers requires the template definitions in the h file itself, if it is used in multiple translation units.So you need to move the template functions to the h file. That should get rid of the link error. You can declare/define the template functions in the cpp file, if you are going to use those functions only with in that translation unit.

Prasannaa
Dec 26 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Zach | last post: by
3 posts views Thread by TR | last post: by
1 post views Thread by toton | last post: by
5 posts views Thread by Mark | last post: by
3 posts views Thread by little.freaky | last post: by
1 post views Thread by giganut | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.