473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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, 363 views)
Sep 27 '05 #1
2 9355
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

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

Similar topics

0
by: Zach | last post by:
Using Dev-C++ 4.9.8.5 I get error: Compiler: Default compiler Executing g++.exe... g++.exe "C:\winroot\src\c++\stupid_tricks\functor_trick.cpp" -o...
3
by: TR | last post by:
Hi I have this very simple piece of code I can't get running (source code below). I'm using the borland C++ compiler 6.0. The error I'm getting is: Unresolved external 'MyMap<int,...
2
by: B_Love | last post by:
Hey! When trying to compile the code for a ordered vector class I get the following error: undefined reference to `WinMain@16' Anyone have any idea what I might be doing wrong? I've been...
1
by: toton | last post by:
In C++ , when the class is declared in the header & not implemented in the cpp, the linker is not giving a error untill the method gets called. How to ensure that all non pure-virtual method...
5
by: Mark | last post by:
Sorry for creating such a newbish topic, but I just can't seem to figure out what the problem is here. // main.cpp #include <cstdlib> #include <iostream> #include "Vector.h" using namespace...
3
by: little.freaky | last post by:
Hello group, I have a problem with template classes and inheritance. I've searched on the internet to find a solution but all the examples look the same as my code (as far as I can tell) and I...
1
by: giganut | last post by:
Hi, I'm using MS VS.Net2003, and I'm getting a linker error in a project for a templated friend function Specifically: template <class Tclass Vec3 {...
7
by: meLlamanJefe | last post by:
The following is an abridged copy of the code I am using in a template function along with the linker error produced during the build. The compiler has no issues with the code, only the linker: ...
1
bajajv
by: bajajv | last post by:
Hi, While implementing CList class, I got linker errors for constructor and destructor. Below is the code - template<class TYPE, class ARG_TYPE = const TYPE&> class CList { protected: struct...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.