I have a template class that I've subclassed with a pointer to it (Decorator pattern). I added a getBase()
call to return the pointer to the base class for any further subclasses. However, when I use that getBase()
and call the base classes only method, I get a linker error that it can't find the symbol for that method in the intervening (Decorator) class?
Like this:
template <typename T> class B {
public:
typedef std::auto_ptr<T> MYFUN(
std::istream&, const std::string&, const std::string& );
public:
B<T>( MYFUN* p );
auto_ptr<T> fun( istream& );
private:
MYFUN *fptr;
};
template <typename T>
class D : public class B<T>
{
D( typename B<T>::MYFUN *fPtr, B<T> *providedBase );
//Looks like B
B* getBase() { return base_ ; }
private:
B* base_;
};
template <typename T>
class Dagain : public class D<T>
{
//Looks like D
auto_ptr<T> fun( istream& );
};
auto_ptr<T>
Dagain::fun( istream& is )
{
this->getBase()->fun( is );
}
Note that there is no definition for fun( istream& )
in D<T>
. The intention is for the client to use the pointer to the base to call B<T>::fun( istream& )
When the client constructs a Dagain
object, the linker says (basically):
Client.o:
(.data.rel.ro. --stuff--
[vtable for D<T>]:
undefined reference to
'D<T>::fun( basic_istream<char, char_traits<char> >&)'
But, I'm not calling D's definition of fun(istream&)
... it doesn't even have one! I'm using the pointer directly to the base class... When I add a definition for D<T>::fun(istream&)
things work, but I don't understand why?