gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Definining template class in .h and implementin it in a .cpp

#175253 - Polaris - Sat Oct 16, 2010 9:15 pm

I have read that you can choose among different methods to have your template class divided into a .h and .cpp. I didn't quite liked any, but i chose the one below.

Code:
#ifndef MYCLASS_H
#define MYCLASS_H

template<class BaseClassType>
class Factory
{
public:
    Factory(void);
    ~Factory(void);
};
#include "Factory.cpp"
#endif


Code:
#include "Factory.h"

template<class BaseClassType>
Factory<BaseClassType>::Factory(void)
{

}
template<class BaseClassType>
Factory<BaseClassType>::~Factory(void)
{

}


The problem is that Visual Studio 2008 Express complains about it. Saying the following:

error C2995: 'Factory<BaseClassType>::Factory(void)' : function template has already been defined

I really can't figure out what the problem is, I found the solution posted in several different places, so I'm guessing it is me being blind to some small syntax error.

#175254 - wintermute - Sat Oct 16, 2010 10:25 pm

You probably want to make a note to avoid the sites or the people that gave you that "solution".

Factory.h includes Factory.cpp and Factory.cpp includes Factory.h - this results in the contents of Factory.cpp being placed in the translation unit twice. Obviously the compiler then chokes. The only way you're going to avoid an error with this code is never to attempt to compile the .cpp which, to my mind, makes it a no brainer to include the template body in the header rather than separating things this way.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#175255 - Polaris - Sat Oct 16, 2010 11:14 pm

Ah, I figured that including the .cpp at the end of the header was just like copying the code in there. I never thought the compiler would attemp to compile the .cpp. Which now that I think about it, has no logical basis :P

Thanks!

#175859 - tom - Sun Feb 20, 2011 11:41 am

This is ridiculous. Just put everything into the header and rely on the compiler to eliminate duplicated template instantiations properly. Microsoft's compiler is by now pretty good at that.

#175861 - Dwedit - Sun Feb 20, 2011 4:33 pm

I thought you had to include full source code for templated classes in the header file.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#175862 - tom - Sun Feb 20, 2011 5:20 pm

Ah, I skipped over the thread and was thinking of something slightly different. Anyway, here's my preferred way to do template classes:

Code:

#ifndef MYCLASS_H
#define MYCLASS_H

template<typename BaseClassType>
class Factory
{
public:
    Factory(void)
   {
    // Function definitions right inside the class declaration.
   }
    ~Factory(void)
   {
   }
};
#endif


I prefer putting the function definitions right into the class declaration. Personally I think this actually declutters the code, and I don't have to maintain function signatures in declarations AND definitions.