g++ "because the following virtual functions are pure" with abstract base class

tdihp picture tdihp · Sep 27, 2012 · Viewed 61.3k times · Source

Here is my example code which produces the error:

struct Impl
{
  int data_size_;
  int find(int var){return 0;}
  int get(int rowid){return 0;}
};

class Container
{
public:
  Container() {}
  virtual ~Container() {}
  virtual int get_size() = 0;
  virtual int get(int rowid) = 0;
};


class SortedContainer : virtual public Container {
public:
  virtual int find(int var) = 0;
};

class ContainerImpl : public Container
{
protected:
  Impl impl_;
public:
  int get_size() {return impl_.data_size_;}
  int get(int rowid) {return impl_.get(rowid);}
};

class SortedContainerImpl
  : public SortedContainer, public ContainerImpl
{
private:
  typedef ContainerImpl Base;
public:
  int find(int var){return Base::impl_.find(var);}
};

ContainerImpl ci;
SortedContainerImpl sci;

it seems "SortedContainerImpl" went wrong while "ContainerImpl" is fine.

g++ complains:

example_b.cpp:42:21: error: cannot declare variable ‘sci’ to be of abstract type ‘SortedContainerImpl’
example_b.cpp:32:7: note:   because the following virtual functions are pure within ‘SortedContainerImpl’:
example_b.cpp:13:15: note:  virtual int Container::get_size()
example_b.cpp:14:15: note:  virtual int Container::get(int)

I inheret SortedContainerImpl from ContainerImpl in order to reuse get_size() and get(int)

I'm not familiar with c++, What's the nature of this problem and How can I fix it?

Thanks all.

Answer

Alok Save picture Alok Save · Sep 27, 2012

In C++ Once you have an pure virtual member function your class becomes abstract class and you cannot create any objects of it.
Such a class is not meant to be instantiable by itself.It is meant to act as an Interface. One would derive from such an abstract class and provide implementations of all the pure virtual functions in the derived class.

Note that your class SortedContainerImpl derives from two classes SortedContainer and ContainerImpl.
SortedContainer in turn derives from Container but it never implements the pure virtual functions.