• Advertisement
Sign in to follow this  

GCC linker and templates

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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<CTest>::CDataSet[in-charge]()' Any ideas what it could be? Thanks

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement