0

I almost solved issues with my code with the help of stackoverflow users but now have different problem. My code now looks like this:

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;

class root
{
public:
    virtual ~root() {}

    virtual root* addA(const root& a) const=0;
    virtual root* addB(const root& b) const=0;
};

class bb;

class aa: public root
{
public:
    aa() { }
    aa(const aa& a) { }

    root* addA(const root& a) const
    {
        return new aa();
    }

    root* addB(const root& b) const
    {
        return new bb();
    }
};

class bb: public root
{
public:
    bb() { }
    bb(const bb& b) { }

    root* addA(const root& a) const
    {
        return new aa();
    }

    root* addB(const root& b) const
    {
        return new bb();
    }
};

int main(int argc, char **argv)
{
}

But when I compile it, it gives errors:

/home/brain/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root* aa::addB(const root&) const’:|
/home/brain/Desktop/Temp/Untitled2.cpp|30|error: invalid use of incomplete type ‘struct bb’|
/home/brain/Desktop/Temp/Untitled2.cpp|15|error: forward declaration of ‘struct bb’|
||=== Build finished: 2 errors, 0 warnings ===|
Brian Brown
  • 3,873
  • 16
  • 48
  • 79
  • @Mat I think the dependency problem can be fixed by putting the definitions of the relevant methods in implementation files. The design is still fishy though. – juanchopanza May 09 '13 at 09:26

2 Answers2

4

For this specific case, you can move the definitions of the member function to after class bb has been defined. OR preferably put them in a separate .cpp file and include the header.

class aa: public root
{
public:
    // ....
    root* addB(const root& b) const;
    // Declaration only
};


class bb: public root
{
public:
    // ....
};

// Here.
inline    // <--- If you define it in the header.
root* aa::addB(const root& b) const
{
    return new bb();
}
stardust
  • 5,918
  • 1
  • 18
  • 20
2

Since compiler doesn't have any idea about class bb so it wont be able to create an object and return it return bb()

It will work if you define the root* addB(const root& b) const outside the class, because at that time it would be able to know the size of class bb.

stardust
  • 5,918
  • 1
  • 18
  • 20
Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51