0

I have two C++ header/source files. In Object.h:

#include "Exception.h"

class SomeException : public Exception {};

class Object {
public:
    someFoo() {
        throw SomeException();
    }
};

And in Exception.h:

#include <exception>
#include "Object.h"

class Exception : public Object, public std::exception {
    // ...
};

The "include once" macro is omitted. The code would not be compiled, because one of Object or Exception will become unknown, so I add a forward declaration header file common.h:

class Object;
class Exception;

And change #include lines to #include "common.h". This skill works for arguments' and returning types, but it fails on inheritance, because C++ refuses to derive an incomplete base class. How to solve it?

Cowsay
  • 71
  • 4
  • 2
    This can be solved by moving someFoo to .cpp file, but looks like incorrect design. How can Object throw SomeException, it should not know anything about it. – Alex F Jul 05 '18 at 05:37
  • 1
    By moving SomeException to Exception.h and moving someFoo to cpp file and removing inclusion of Exception.h from Object.h you can solve the problem – kreuzerkrieg Jul 05 '18 at 05:49
  • I've added a new answer to the duplicate. You can use this technique to solve your problem. – geza Jul 05 '18 at 07:06

0 Answers0