When declaring this:
class Bar;
class Foo {
public:
Bar b1;
};
Think about it, Foo
has no idea what Bar
"looks" like, it just knows that Bar
is a class, so what the sizeof Bar
and therefore Foo
should be?
On the other hand, when using a pointer, it doesn't need to know the size of the object, because the sizeof pointer is fixed and known, therefore you could do what @Chris proposed.
But since C++17, std::vector
also accepts incomplete type so you could define this in the header:
bar.h
#include <vector>
class Foo;
class Bar
{
public:
std::vector<Foo> f1;
};
foo.h
#include "bar.h"
class Foo
{
public:
Bar b1;
};
Then include both bar.h and foo.h in both .cpp file.