Sign in to follow this  

virtual function problem

This topic is 3630 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

I tried to implement an templated AssetManager i found discribed in a a tuturial at http://www.gamedev.net/reference/programming/features/cppasset/page2.asp . But i keep getting some problems: My classes are like this: Manager.h:
namespace foo{
template<typename Asset>
class Manager{
public:
Manager();
virtual ~Manager();
virtual bool load(std::string name) = 0;
//other unimportant stuff
}
}


Manager.cpp
#include "Manager.h"

namespace foo{

template<typename Asset>
Manager<Asset>::Manager(){
//..
}
template<typename Asset>
Manager<Asset>::~Manager(){
deleteAll();
}
}


TextureManager.h
#include "Texture2D.h"
#include "Manager.h"

namespace foo{
class TextureManager:public Manager<Texture2D>{
public:
TextureManager(){
}
~TextureManager(){
}
bool load(std::string name){
//..
return true;
}
}
}

and i get this errors: undefined refrence to foo:Manager<Texture2D>::~Manager() undefined refrence to foo:Manager<Texture2D>::Manager() does anyone know what i'm doing wrong. I'm using g++ 4.1.3 on ubuntu Gutsy

Share this post


Link to post
Share on other sites
Unless your compiler supports the export keyword, and I'm pretty sure that gcc 4.1.3 doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

Share this post


Link to post
Share on other sites
If you like the mental separation of the interface from its guts, you can #include the implementation at the bottom of the header (before you close your #include guard). It's quite common to give the implementation file something like a ".impl" extension, rather than ".cpp".

Share this post


Link to post
Share on other sites

This topic is 3630 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this