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
Related
This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 3 years ago.
If I have a following situation:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Inside A" << endl;
}
};
int main() {
A a();
return 0;
}
Why is the constructor not invoked?
If something looks like a function declaration, the C++ standard requires it be treated as a function declaration.
A a(); does not default-construct an object a of type A. It declares a function a that takes no input parameters and returns an A object as output.
To default-construct a variable a, you need to drop the parenthesis:
A a;
Or, in C++11 and later, you can use curly braces instead of parenthesis:
A a{};
This question already has answers here:
Do class functions/variables have to be declared before being used?
(5 answers)
Closed 5 years ago.
class cl {
public:
cl(int i) { val=i; }
int val;
int double_val() { return val+val; }
};
Variable val is declared after the constructor, which assigns it. But still this code works. Isn't 'val' out of scope for constructor?
The full definition of the class is available to its members. So val is actually declared before the constructor's implementation.
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:
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.
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++?