Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inheriting from a template class

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
i'm using vs2005, working on a c++ application. i'm trying to create a base template class and inherit from that, but the project returns an error when it compiles. for example, the base class would look like...

Code:
template <class T>
class BaseTable
{
public:
	BaseTable(void);
	~BaseTable(void);
};

template <class T>
BaseTable<T>::BaseTable(void)
{
}

template <class T>
BaseTable<T>::~BaseTable(void)
{
}

then, the class that inherits from this would look like...

Code:
#include "basetable.h"

class AccessLevels :
	public BaseTable<int>
{
public:
	AccessLevels(void);
	~AccessLevels(void);
};

#include "AccessLevels.h"

AccessLevels::AccessLevels(void)
{
}

AccessLevels::~AccessLevels(void)
{
}

everything compiles fine when i just have the base template class. also, if i have the "accessLevels" class with the constructor and destructor removed (just the shell), it compiles fine. as soon as i include the constructor, the build process fails with a linker error...

Error 1 error LNK2019: unresolved external symbol "public: __thiscall BaseTable<int>::BaseTable<int>(void)" (??0?$BaseTable@H@@QAE@XZ) referenced in function "public: __thiscall AccessLevels::AccessLevels(void)" (??0AccessLevels@@QAE@XZ) AccessLevels.obj


how do i fix this? i've asked a few other programmers at my company and nobody's had a clue.

thanks,

glenn
 
found the answer. i'm not sure of the full explanation, though it seems like a microsoft c++ defect to me. at any rate, the fix was to collapse the base class template file from its .CPP and .H files into a single .H file. after that, project compiles without error.
 
I don't know if VS2005 supports export. If you want to keep the template body separate, put export before template.

VS2003 didn't support export.
 
This is a common problem. You can call it a defect, but most compilers don't support export (Comeau might be the only one that does). Putting the code into the header is one solution. Another is to leave the code separated and #include the cpp file at the bottom of the header file to mimic keeping everything in one. Some people prefer to give the source file an .inl extension to indicate that it is being included and isn't a normal source file.
 
I don't really see the point in including a .cpp file in a header? In that case the .cpp file essentially becomes another header with a .cpp extension. It's much easier to just keep it all in the .h file.
 
The point would be to keep interface and implementation separated and make the transition to a compiler that eventually supports export easier.

I generally put everything in the header file myself, but the alternative is there and acceptable for those who prefer the separation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top