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

66,402

Solution 1

Your SortedContainerImpl class has two separate Container base classes. One is virtual (via the SortedContainer class) and the other is non-virtual (via the ContainerImpl class).

SortedContainerImpl has concrete implementations of Container::get_size() and Container::get(int) for the base that comes in from ContainerImpl, but not for the virtual base that comes in via SortedContainer.

One way to fix the problem is to give concrete implementations in SortedContainerImpl:

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

  int get_size() {return ContainerImpl::get_size();}
  int get(int rowid) {return ContainerImpl::get(rowid);}
};

Another way would be to make Container a virtual base class of ContainerImpl, so SortedContainerImpl would only get the one, virtual, base Container:

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

Solution 2

In C++, once you have a pure virtual member function, your class becomes an abstract class and you cannot create any objects from 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.

Share:
66,402
tdihp
Author by

tdihp

Updated on September 27, 2020

Comments

  • tdihp
    tdihp over 3 years

    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.

  • tdihp
    tdihp over 11 years
    Thanks, it seems your answer is right. Should I really just make all interface classes virtually inherited everywhere?