So I am creating a project that involves having vectors nested inside one another, but the structure won't compile. I think this is a case of circular dependency, but all of the fixes I've seen seem to only apply to code involving header files and separate translation units.
#include <iostream>
#include <vector>
class A {
public:
virtual ~A();
};
// The following forward declaration statements didn't solve the
// compiler error:
// class C;
// class C : public A;
class B : public A {
std::vector<A*> Avector;
public:
void addC(C* Cin){Avector.push_back(Cin);}
~B();
};
class C : public A {
std::vector<A*> Avector;
public:
void addB(B* Bin){Avector.push_back(Bin);}
~C();
};
int main() {
B b;
C c;
b.addC(&c);
c.addB(&b);
}
I've tried forward declaring with
class C;
Class C : public A;
But none of it seems to work.
The error I get is:
\test\test\source.cpp(15): error C2061: syntax error : identifier 'C'
\test\test\source.cpp(15): error C2065: 'Cin' : undeclared identifier
Is this sort of code implementable?