Basic Forward declaration not working [duplicate] - c++

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.

Related

Why doesn't this class forward declaration compile in C++? [duplicate]

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

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.

Incomplete type only if not declared as pointer [duplicate]

This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 7 years ago.
I'm having an object declared in a class:
class B;
class A{
B object;
};
If I declare it B object; I get "object has incomplete type". If I declare it "B* object", the compilation ends successfully.
Why does that happens?
I'm using C++11;
When declared as B object;, A needs the complete definition of B so the compiler knows how large it is, and therefore how large A is. When declared as a pointer, this information is not needed, because all pointers are the same size on a given platform.

Instance of class and function return type confusion [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 10 years ago.
I cannot do this:
class A
{
public:
A()
{
}
};
A a1();
Because A a1(); looks like a function prototype.
But I can do this:
class B
{
public:
B(std::string argument)
{
std::cout << argument;
}
};
B b1("Text");
These two things are essentially the same except the compiler is able to distinguish B b1("Text"); as NOT being a function prototype, because some data is passed in the parenthesis.
Is there any reason why the brackets must be omitted for A, or is the reason because the compiler thinks it is a function definition?
That's exactly it, and it's known as most vexing parse. The reason is that if A a1(); was treated as an object declaration, you wouldn't be able to declare a function with that prototype. And you want to be able to declare a function, right?
B b1("Text"); works because it can't be treated as a function prototype, but, for example, B b(A()); can and will.

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++?