Łukasz Szcześniak
Łukasz Szcześniak

Reputation: 1444

Deriving from pure virtual template class

I belive my problem is simple, yet I cannot manage to overcome it.

I have abstract template class aghContainer and child template class aghVector. I am trying to make aghVector object, but I get an error that I cannot create objects of abstract class. I am pretty sure that I implemented all methods, but maybe I miss something...

aghContainer:

template <typename T>
class aghContainer {
public:
    virtual ~aghContainer() {}
    virtual void append(T const&) = 0;
    virtual void append(aghContainer<T> const& right) = 0;
    virtual bool replace(const int, T const&) = 0;
    virtual T& at(const int) const = 0;
    virtual int size(void) const = 0;
    virtual bool remove(const int) = 0;
    virtual void clear(void) = 0;
    virtual bool isEmpty(void) = 0;
    virtual int indexOf(T const& _value, int _from = 0) const = 0;
    virtual bool contains(T const& _value, int _from = 0) const = 0;
    virtual void print(ostream&) const = 0;
    virtual bool equal(aghContainer<T> const& right) const = 0;
    virtual bool operator==(aghContainer<T> const& right) const;
    virtual bool operator!=(aghContainer<T> const& right) const;
    virtual T& operator[](const int n) const;
    virtual aghContainer<T>& operator+=(T const& element);
    virtual aghContainer<T>& operator+=(aghContainer<T> const& right);
    virtual aghContainer<T>& operator<<(T const& element);
    virtual aghContainer<T>& operator<<(aghContainer<T> const& right);
    friend ostream& operator<<(ostream&, aghContainer<T> const& right);
};

aghVector:

template <typename T>
class aghVector :
    public aghContainer<T> {
public:
    aghVector();
    ~aghVector();
    void append(T const&);
    void append(aghContainer<T> const& right);
    bool insert(const int, T const&);
    bool replace(const int, T const&);
    T& at(const int) const;
    int size(void) const;
    bool remove(const int);
    void clear(void);
    bool isEmpty(void);
    int indexOf(T const& _value, int _from = 0);
    bool contains(T const& _value, int _from = 0) const;
    void print(ostream&) const;
    bool equal(aghContainer<T> const& right) const;

private:
    T* vector;
    unsigned int elements;
    void destroyVector();
};

Error:

'aghVector': cannot instantiate abstract class Data-Container G:\CPP\Data-Container\ex3main.cpp 101

Upvotes: 1

Views: 278

Answers (2)

Peter
Peter

Reputation: 36617

A couple of problems I can see on a quick look.

In aghContainer

 void append(aghContainer<T> const& right) = 0;

and in aghVector

  void append(aghVector<T> const& right);

actually overloads the function with a different argument type (since aghVector is a distinct type from aghContainer. The inherited append() will be hidden and not overridden.

A similar problem for equal().

The declaration of indexOf() in aghContainer is const, and the one in aghVector is not. Therefore, again, the version in aghVecvtor hides the inherited function, and does not override it.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409384

From the aghContainer class you have the function

void append(aghContainer<T> const& right) = 0;

Then in the aghVector you have this function:

void append(aghVector<T> const& right);

The problem is that those are two different functions, which means you don't override the parent class append function. leading to aghVector still being an abstract class.

Upvotes: 2

Related Questions