samedi 16 décembre 2017

Custom container for children classes c++

I'm trying to make a program that changes and compares fields of different documents. There is one base class with virtual functions and some children classes. For convenience I want to put objects of children classes into custom container that represents a linear stack. Here is the header code:

template <class T>

class DocContainer{
public:
    struct ListElement{ // the stack itself
        T obj;
        ListElement *next;
    };
    DocContainer();
    ~DocContainer();

    void pop_top();
    T &get_top() const;
    T &operator[](int);
    void push_top(const T &);
    int size() const;
    void clear();

private:
        ListElement *first;
        int num;
};

I tried to google how to actualize a container for objects of different types and found out that I'm supposed to contain pointers to parent class using the polymorphism. But when I use it like this:

Passport *pass = new Passport;
// some code
DocContainer<Document*> DC;
DC.push_top(dynamic_cast<Document*>(pass));

The compiler warns about undefined references to container functions. There is something surely wrong, but I couldn't find any clear answer. What's the mistake? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire