Thread: Polymorphism with templated classes

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Polymorphism with templated classes

    I'm trying to define a variable type at runtime based on user input, so what I did was created an abstract base class Variable and derived a templated class from it.

    Code:
    class Variable
    {
    public:
         virtual ~Variable() = 0;
    };
    
    template<class T>
    class Data : public Variable
    {
    public:
         Data();
         virtual ~Data();
    protected:
         T value;
    };
    This way I can get the input, determine what type to use, and then do something like

    Code:
    Variable* var;
    var = new Data<int>;
    and then add all the variables to a map<string,Variable*>. My problem is that when I try to compile the linker gives me this error:

    Error LNK2019: unresolved external symbol "public: _thiscall Data<int>:ata<int>(void)" (??0?Data@H@@QAE@XZ) referenced in function "protected: void _thiscall Console::def(void)" (?def@Console@@IAEXXZ)
    My constructor for Data looks like this

    Code:
    template<class T>
    Data<T>::Data()
    {
    }
    Why isn't the linker able to use my constructor? Is polymorphism even possible with a derived templated class?
    Last edited by LyTning94; 01-11-2013 at 11:06 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Thanks for the link, it works great now! That was a lot simpler of a solution that I expected.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Actually, the correct solution here could quite possibly be to delete the constructor and its prototype. Having said that, the class currently also has a destructor that does nothing, and a member variable with no way to access it, so clearly this is not the real code.

    For template code you're often better off declaring the entire method inside the declaration of the class.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Personally, with the template implementation file, I prefer to use the extention *.hpp. Depending on the build system, this can stop the compiler from needlessly building and linking it, but it maintains the separation between declaration and definition that is the usual practice with C++ classes.

    (*.hpp is one of the standard C++ header extensions, along with *.h and *.hxx, that any progam that deals with C++ will recognize).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templated Classes with Templated Functions
    By -EquinoX- in forum C++ Programming
    Replies: 9
    Last Post: 11-22-2009, 10:44 PM
  2. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  3. Templated classes...
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2006, 04:31 PM
  4. Templated singleton classes
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2006, 04:06 AM
  5. templated classes
    By homeyg in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2006, 09:07 AM