#pragma once
#ifndef _A_H_
#define _A_H_
#include "b.h"
class B;
usingnamespace std;
class A {
public:
B *b;
int i;
A(int ii);
A(const A &aa);
~A();
void display1()const;
};
#endif
#pragma once
#ifndef _A_H_
#define _A_H_
class B;
usingnamespace std;
class A {
public:
B *b;
int i;
A(int ii);
A(const A &aa);
~A();
void display1()const;
};
#endif
#pragma once
#ifndef _B_H_
#define _B_H_
#include <iostream>
usingnamespace std;
class A;
class B{
public:
A *a;
int i;
B(int ii);
B(const B &bb);
~B();
void display2()const;
};
#endif
If A's ctor creates a B, and B's ctor creates an A....
then A makes a B which makes an A which makes a B which makes an A which makes a B, etc, etc, etc. It will loop until you run out of stack space and the program explodes.
EDIT: Also your copy constructor is ill formed and your assignment operator is nonexistant. This has potential memory leaks.
#pragma once
#ifndef _A_H_
#define _A_H_
class B;
usingnamespace std;
class A {
public:
B *b;
int i;
A(int ii);
A(const A &aa);
A &operator=(const A &aa);
~A();
void display1()const;
};
#endif
#pragma once
#ifndef _B_H_
#define _B_H_
#include <iostream>
usingnamespace std;
class A;
class B{
public:
A *a;
int i;
B(int ii);
B(const B &bb);
B &operator=(const B &bb);
~B();
void display2()const;
};
#endif
If A's ctor creates a B, and B's ctor creates an A....
then A makes a B which makes an A which makes a B which makes an A which makes a B, etc, etc, etc. It will loop until you run out of stack space and the program explodes.
You simply can't do this. It's logically impossible. You're trying to create an infinite amount of objects.
yes, you can have pointers to other objects, but you can't have them each create each other.
Forget about the code and think about the logic. If every A has a unique B, and every B has a unique A.... then you have an infinite amount of objects. As soon as you make an A, it makes a B which makes another A which makes another B which makes yet another A which makes yet another B, etc, etc, etc.
What you're trying to do is similar to this... also mentioned in that article:
My article wrote:
1 2 3 4 5 6 7 8
// a.h (guarded)
#include "b.h"
class A
{
B b; // B is an object, can't be forward declared
};
1 2 3 4 5 6 7 8
// b.h (guarded)
#include "a.h"
class B
{
A a; // A is an object, can't be forward declared
};
You may note, however, that this situation is conceptually impossible. There is a fundamental design flaw. If A has a B object, and B has an A object, then A contains a B, which contains another A, which contains another B, which contains another A, which contains another B, etc, etc. You have an infinite recursion problem, and either class is simply impossible to instantiate.
What you CAN do, is you can have A create a B, and then have B point to an existing A (without creating a new one). So something like this would work:
1 2 3 4 5 6 7 8 9 10 11 12
class A {
B* b;
A() {
b = new B;
};
class B {
A* a;
B(A* aa){
a = aa;
}
};
Now this is OK because B isn't creating a new A, it's simply using an existing A.