Here is a problem of circular dependency. I want to allow a class B ctor friend access to private members of a class A object. But in order to tell class A this, class A needs to know about the existence of the class B::B(const A&) ctor, so a simple forward declaration will not work.
Code:
//A.h:
#ifndef A_H
#define A_H
#include "B.h"
class A {
public:
A(int i);
friend B::B(const A &);
private:
int _i;
};
#endif
//B.h:
#ifndef B_H
#define B_H
#include "A.h"
class B {
public:
B(const A &);
private:
int deg;
};
#endif
//A.cpp:
#include "A.h"
A::A(int i) : _i(i) { }
//B.cpp:
#include "B.h"
B::B(const A &a) : deg(a._i) { }
My basic approach was to have each class header include the other class header so that they each know of the other's entire structure. But inevitably one header comes before the other (of course) so it seems that it is simply impossible to have each class know of the other classes' complete definition as it is being compiled.
So there is no main() here. Errors are displayed with just: