Skip to main content
GameDev.net gamedev.net
🔒 Locked

GCC linker and templates

Started by OklyDokly Mar 30, 2005 at 7:09 AM 4 replies 1k views
Original Post
OklyDokly
OklyDokly
Hi I'm getting linker problems when I'm trying to implement my second template class in my project using the linux GCC linker and eclipse with CDT. I'm just wondering whether anyone's ever had similar problems. Here's the code: DataSet.h

#ifndef _DATASET_H
#define _DATASET_H

template <class T> class CDataSet
{
public:
	CDataSet();
};

#endif //_DATASET_H_
DataSet.cpp

#include "DataSet.h"

template <class T>
CDataSet<T>::CDataSet()
{
}
Main.cpp

#include "DataSet.h"
class CTest
{
};

int main( int argc, char* argv )
{
CDataSet<CTest> dataSet;
}
The error I get is: undefined reference to `CDataSet::CDataSet[in-charge]()' Any ideas what it could be? Thanks
Skizz
Skizz
With templates, the code for all the functions must exist in the current translation unit for the code to compile. The upshot being that you need to declare the code in the template definition and not in a separate source file.

So, have:
template <class T> class CDataSet{public:	CDataSet()        {            // put code here        }};

and get rid of DataSet.cpp.

Skizz
OklyDokly
OklyDokly
Thanks, I never knew that. Learn a new thing every day :)

It's a little odd though, I had a linked list template up and running with the same code structure, the compiler didn't seem to complain about that one.

Guess I better modify that too :s
SirLuthor
SirLuthor
Or as I prefer to do it, take the code of DataSet.cpp, make a header file called DataSet_impl.hpp, put all the function definitions in there, and include it at that bottom of DataSet.hpp. That way, you can still look at the main header file, DataSet.hpp and get an overview of what methods are available, without having to wade through loads of implementation.

This is, however, my personal opinion, and is not necessarily the best, or even a good way to do things [smile]

[EDIT] Didn't see your reply.. Just so you know, the linker won't complain about undefined references until you make an instance of the templated object, because templates are compiled as necessary so if you don't include an instance of the object somewhere in your code, it will not be compiled, and thus, you won't get linker errors. OR at least, that's what I've been told.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
OklyDokly
OklyDokly
Thanks for your replies.

I've just scrolled to the bottom of my linked list template header and found a

#include "LinkedList.cpp"


at the bottom of the file.

Looks like I've encountered this problem before and forgotten about it :s
SirLuthor
SirLuthor
You shouldn't make a habit of #include'ing .cpp files. In this case, sure, it may work, but keep this in mind, all .cpp files in a project are compiled by default, so any time you include a .cpp file, you will be compiling it again, and this will probably lead to problems in the future, like re-defined functions, etc. Make it a header file.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.