C++: How to create two classes using each other? [duplicate] - c++

This question already has answers here:
How to create two classes in C++ which use each other as data?
(2 answers)
C++ Circular Dependency in Header Files
(1 answer)
Closed 5 years ago.
I want to create two classes using each other. This is the code:
class B;
class A {
public:
A(B* b) : b(b) {}
void Foo() {
b->data++;
}
B* b;
};
class B {
public:
void Boo() {
A a(this);
a.Foo();
}
int data = 0;
};
int main()
{
B b;
b.Boo();
}
When I compile it in visual Studio 2015, I got the error:
error C2027: use of undefined type 'B'
which points to line six: b->data++;
I have read the problem here, but I have used forward declarations so A should know the existence of B. Also, I don't "have two classes directly contain objects of the other type" because only B contains A but A does not contain B; it uses a pointer to B. Also, my code doesn't seem essentially different from the answer here since there the World class contains objects of Agent and the two classes use each other. But my code just can't compile. So I think my issue is not duplicate. Could you please help me work around this mutual-use issue? Thanks a lot.

Related

C++ compiler option to allow the use of incomplete type in classes that make use of each other? [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 years ago.
Is there a compiler option that will allow the following code to compile, without moving the function definition to the bottom?
class first{
int a;
void func(second b){}
};
class second{
int a;
void func(first b){}
};
Nope, not when you pass it by value. If you passed it by pointer or by reference you could.

compiler cannot compile inheritance c++ [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 6 years ago.
Suppose i have a header which contain these two classes
class A:public class B{
// code
};
class B
{
protected:
A a_object;
};
when the compiler compiles this include file,when it comes to Class A ,it sees class A inherits from B but it doesn't reach to Class B definition.So it gives an error. and if i reverse the order of both classes,it gives error due to a_object as it doesn't see Class A definition.
So how to solve this problem? and assume that i restricted to this include file to have class A and B definitions.
Thanks
Here is a simplified version of your problem:
class X {
X x;
};
A class cannot embed an object of its own type.
Specifically, your class B embeds an object of type A, which by inheritance is also of type B.
If you really have to have a hierarchy like that you can do something like the following.
class A;
class B
{
protected:
A* a_object;
};
class A: public B {
// code
};

Basic Forward declaration not working [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
What are forward declarations in C++?
(8 answers)
Closed 6 years ago.
class B;
class A
{
B b;
A();
};
class B
{
A a;
B();
};
I have two classes as follows. For some reason, even though I forward declare class B, I have an error that says that:
field 'b' has an incomplete type!
Why is this the case?
When you declare an instance of a class, you need the class to be fully defined since the compiler needs to know its size.
If you only declare a pointer or a reference then the compiler only needs the symbol name.
So if you change A::b to a pointer (i.e. B* b) or reference (B& b) then it will work.

How can I create two different classes that have references to eachother within their code? [duplicate]

This question already has answers here:
Two classes that refer to each other
(2 answers)
Closed 8 years ago.
How can I create two different classes that have references to eachother within their code in C++?
It would look something like this;
class A
{
B b;
}
class B
{
A a;
}
I get an error on the line of the
B b;
within class A stating;
error: 'B' was not declared in this scope
Rest easy in that I am not declaring a
B b = new B;
in A, nor am I declaring a
A a = new A;
in B, as I'm aware it would cause a StackOverflowException. I will use getters and setters to manipulate the data.
Use forward declarations, and make the members pointers. It's easiest with two different headers, although it could be done in one:
// A.h
class B;
class A
{
B* m_pB;
};
And:
// B.h
class A;
class B
{
A* m_pA;
};
Then #include both headers in each .cpp file (A.cpp and B.cpp).
This is probably what you want:
class B;
class A
{
B* b;
}
class B
{
A* a;
}
Note that the class B is forward-declared as a class so that the compiler knows what it is when defining A. Also note that A::b is a pointer to B and B::a is a pointer to A (what you probably mean by "reference" if you come from a Java background). If A::b was declared to be of type B and B::a was declared to be of type A then each A object would literally contain an entire B object inside it and vice versa, which I'm guessing is not what you wanted (and is obviously impossible anyway).

does not name a type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Resolve circular dependencies in c++
What is forward declaration in c++?
I have two classes A and B. I need to have a field in each which is a pointer to an object of the other class. I get "does not name a type" since the definition of the class is yet to appear. For example:
class A{
B* b;
}
class B{
A* a;
}
gets me "B "does not name a type" at the second line.
Use forward declarations:
class B;
class A {
B* b;
};
class B {
A* a;
};
This way you're telling the compiler that B is going to be declared later on and that it should not worry. See this for more info: Forward declaration
Forward declarations is key to you question , here is the link
What is forward declaration in c++?