1

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

Łukasz Szcześniak
  • 1,417
  • 11
  • 23

2 Answers2

2

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Oh. I was expecting that. Although, changing aghVector in this methods to aghContainer doesn't fix error. And how should I overcome, that I cannot put aghVector in this declaration? I would like this method to work exsclusively with vectors. – Łukasz Szcześniak May 07 '16 at 00:12
2

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.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • Oh god! You are right! I didn't see I missed const in indexOf(). I shouldn't write code so late at night. But I have a chance to ask, so: how can I force append() in aghVector to take only aghVector, if I must write aghContainer in method header? – Łukasz Szcześniak May 07 '16 at 00:21
  • 1
    Well ..... I'd step back further from that .... By using inheritance, you are requiring `aghVector::append()` to accept any object of type (derived from) `aghContainer`. The fact you're now trying to limit that to only work with `aghVector` suggests inheritance is a really bad design decision. – Peter May 07 '16 at 00:43