title: Abstract class : invalid abstract return type for member function ‘virtual...’
body: <p>In my program I have such class hierarchy:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  class aa;
  class bb;
  
  class root
  {
  public:
      virtual ~root() {}
      virtual root add(const aa&amp; a) const=0;
      virtual root add(const bb&amp; a) const=0;
  };
  
  class aa: public root
  {
  public:
      aa() { }
      aa(const aa&amp; a) { }
  
      virtual root add(const aa&amp; a) const
      { return root(new aa()); }
      virtual root add(const bb&amp; a) const
      { return root(new bb()); }
  };
  
  class bb: public root
  {
  public:
      bb() { }
      bb(const bb&amp; b) {}
  
      virtual root add(const aa&amp; a) const
      { return root(new bb()); }
      virtual root add(const bb&amp; a) const
      { return root(new bb()); }
  };
  
  int main(int argc, char **argv)
  {
  }
  </code></pre>
  
  <p>but Im still getting errors in compilation process. I cant change my class hierarchy but it is possible to make something I want here?</p>
  
  <p><strong>EDITED CLASS:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  class root
  {
  public:
      virtual ~root() {}
      virtual root add(const root&amp; a) const=0;
      virtual root add(const root&amp; b) const=0;
  };
  
  class aa: public root
  {
  public:
      aa() { }
      aa(const aa&amp; a) { }
  
      virtual root add(const root&amp; a) const
      { return root(new aa()); }
      virtual root add(const root&amp; b) const
      { return root(new bb()); }
  };
  
  class bb: public root
  {
  public:
      bb() { }
      bb(const bb&amp; b) {}
  
      virtual root add(const root&amp; a) const
      { return root(new bb()); }
      virtual root add(const root&amp; b) const
      { return root(new bb()); }
  };
  
  int main(int argc, char **argv)
  {
  }
  </code></pre>
  
  <p>Errors for edited class:</p>
  
  <pre><code>/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘virtual root root::add(const root&amp;) const’ cannot be overloaded|
  /home/brian/Desktop/Temp/Untitled2.cpp|10|error: with ‘virtual root root::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|10|error: invalid abstract return type for member function ‘virtual root root::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   because the following virtual functions are pure within ‘root’:|
  /home/brian/Desktop/Temp/Untitled2.cpp|10|note:     virtual root root::add(const root&amp;) const|
  /home/brian/Desktop/Temp/Untitled2.cpp|11|error: invalid abstract return type for member function ‘virtual root root::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|20|error: invalid abstract return type for member function ‘virtual root aa::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|22|error: invalid abstract return type for member function ‘virtual root aa::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|22|error: ‘virtual root aa::add(const root&amp;) const’ cannot be overloaded|
  /home/brian/Desktop/Temp/Untitled2.cpp|20|error: with ‘virtual root aa::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root aa::add(const root&amp;) const’:|
  /home/brian/Desktop/Temp/Untitled2.cpp|20|error: invalid abstract return type for member function ‘virtual root aa::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|21|error: cannot allocate an object of abstract type ‘root’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root aa::add(const root&amp;) const’:|
  /home/brian/Desktop/Temp/Untitled2.cpp|22|error: invalid abstract return type for member function ‘virtual root aa::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|23|error: cannot allocate an object of abstract type ‘root’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected primary-expression before ‘(’ token|
  /home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected type-specifier before ‘bb’|
  /home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected ‘)’ before ‘bb’|
  /home/brian/Desktop/Temp/Untitled2.cpp|32|error: invalid abstract return type for member function ‘virtual root bb::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|34|error: invalid abstract return type for member function ‘virtual root bb::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|34|error: ‘virtual root bb::add(const root&amp;) const’ cannot be overloaded|
  /home/brian/Desktop/Temp/Untitled2.cpp|32|error: with ‘virtual root bb::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root bb::add(const root&amp;) const’:|
  /home/brian/Desktop/Temp/Untitled2.cpp|32|error: invalid abstract return type for member function ‘virtual root bb::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|33|error: cannot allocate an object of abstract type ‘root’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root bb::add(const root&amp;) const’:|
  /home/brian/Desktop/Temp/Untitled2.cpp|34|error: invalid abstract return type for member function ‘virtual root bb::add(const root&amp;) const’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  /home/brian/Desktop/Temp/Untitled2.cpp|35|error: cannot allocate an object of abstract type ‘root’|
  /home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
  ||=== Build finished: 38 errors, 0 warnings ===|
  </code></pre>