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++?
Related
This question already has answers here:
C++ class declaration after using it
(3 answers)
What are forward declarations in C++?
(8 answers)
Closed 3 months ago.
I'm sure that this has been asked, but I cannot find the question or answer, so here is the minimal code I tried to compile.
// goof4.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
class A;
class B
{
public:
A func() { return A{}; }
};
class A
{
};
int main()
{
B b;
auto a = b.func();
}
The declaration of B::func gives a "use of undefined type 'A' Error C2027 in MSVC 2022 using /std:c++20. I would have thought that the forward declaration of "class A" would have allowed the compiler to work on B::func until such time as class A was defined. Any help?
Because you have the function body using the (at that point) undefined type A in the class B itself and in a function body the type must already be defined.
just do A funct(); in the class B itself
and put the function body and after defining A,
A B::funct() { return A{}; }
https://ide.geeksforgeeks.org/2db37ea7-a62c-487b-8af5-10af8cebc3c6
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
};
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.
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).
This question already has answers here:
What is the difference between "struct" and lack of "struct" word before member of a struct
(2 answers)
Closed 8 years ago.
I faced this in a code I was reading:
class myClass* holding;
Is this means the same as this below:
myClass* holding;
Thanks,
EK
If at that point myClass is known as a valid class name (i.e. it was declared before) then yes, both declarations are equivalent. It should be noted though that the class name can be hidden by, say, a variable name, in which case using the elaborated version class myClass will allow you to work around the hiding and make sure that you refer to the class name specifically, as in the following example
class C {};
int main() {
int C;
C a; // <- invalid
class C b; // <- OK
}
If at that point name myClass is not known, then the second version simply won't compile. The first version will compile and will introduce a forward declaration of class myClass (see LihO's answer)
class myClass* holding;
is rather equivalent to:
class myClass; // letting compiler know that there is type myClass
myClass* holding;
unless you are using class keyword to resolve the ambiguity of myClass (see the other answer).