By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,016 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,016 IT Pros & Developers. It's quick & easy.

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

P: 1
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, 338 views)
Sep 27 '05 #1
Share this Question
Share on Google+
2 Replies


P: 1
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

P: 21
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.