c++ - Is there any way round to make this template specialisation link? -
i have tried following hierarchy , doesn't link. call c->execute()
not seen since seems masked execute
in derived
, doesn't find adequate type use. have thought if there problem, have show @ compilation. have tried gcc 4.8 online , borland compiler on windows. error message similar in both cases. in order of:
/tmp/ccrt5mny.o:(.rodata._ztv7derivedi5typece[_ztv7derivedi5typece]+0x10): undefined reference `derived<typec>::execute()' collect2: error: ld returned 1 exit status
i grateful pointers.
#include <iostream> class basetype { public: basetype() {} virtual void execute() { std::cout << "basetype execute..." << std::endl; }; protected: }; struct typea; struct typeb; struct typec; class derived2 : public basetype { public: derived2() : basetype() {} virtual void execute() { std::cout << "derived execute2" << std::endl; } protected: }; template <typename t> class derived : public basetype { public: derived() : basetype() {} virtual void execute(); protected: }; template <typename t> class grandchild : public derived<t> { public: grandchild() : derived<t>() {} void execute(); }; template<> void derived<typea>::execute() { std::cout << "derived execute... typea" << std::endl; } template<> void derived<typeb>::execute() { std::cout << "derived execute... typeb" << std::endl; } template<> void grandchild<typec>::execute() { std::cout << "derived execute... typec" << std::endl; } int main() { basetype* = new derived<typea>(); basetype* b = new derived<typeb>(); basetype* c = new grandchild<typec>(); basetype* d2 = new derived2(); a->execute(); b->execute(); c->execute(); d2->execute(); delete a; delete b; delete c; delete d2; }
derived<typec>::execute()
must defined if never call it, since you're instantiating class template. try adding empty definition, or 1 throw
s, etc. , program should link , work expect.
Comments
Post a Comment