incomplete type error - c++

Im trying to make class A a friend of class B.
class B;
class A{
public:
void show(const B&); // ##1## but this one works fine
B ob;// error incomplete type
};
class B{
public:
int b;
B():b(1){}
friend class A;
};
so my question why it's incomplete type? I thought that when I did class B it's like a prototype of a function which tell the compile there is a definition somewhere in the code.
also in the code above at ##1## why this is possible ?

No, that's a forward declaration and does not define a full type. You'll need to have a full definition of B before A, if you want to keep the member as an object and not pointer.
One of the reason for this is that the size of the class B must be known to A, since A's size depends on B.
I suggest you #include "B.h" in A.h.
EDIT: clarification:
struct A;
struct B
{
A foo();
void foo(A);
void foo(A&);
void foo(A*);
A* _a;
A& __a;
A a; // <--- only error here
};

Related

error: member access into incomplete type : forward declaration of

I have two classes in the same .cpp file:
// forward
class B;
class A {
void doSomething(B * b) {
b->add();
}
};
class B {
void add() {
...
}
};
The forward does not work, I cannot compile.
I get this error:
error: member access into incomplete type 'B'
note: forward declaration of 'B'
I'm using clang compiler (clang-500.2.79).
I don't want to use multiple files (.cpp and .hh), I'd like to code just on one .cpp.
I cannot write the class B before the class A.
Do you have any idea of how to resolve my problem ?
Move doSomething definition outside of its class declaration and after B and also make add accessible to A by public-ing it or friend-ing it.
class B;
class A
{
void doSomething(B * b);
};
class B
{
public:
void add() {}
};
void A::doSomething(B * b)
{
b->add();
}
You must have the definition of class B before you use the class. How else would the compiler otherwise know that there exists such a function as B::add?
Either define class B before class A, or move the body of A::doSomething to after class B have been defined, like
class B;
class A
{
B* b;
void doSomething();
};
class B
{
A* a;
void add() {}
};
void A::doSomething()
{
b->add();
}

How do I access a private constructor in a separate class?

I'm writing a library in C++. I have two classes in my library, A and B. I want to hide the A() constructor from any code that references my library. I also want class B to be able to call the A() constructor.
I come from a C# background and remember little of my C++. In C#, I would simply declare the A() constructor as internal. I've read that the closest way to do this in C++ is a combination of friend declarations and forward-declarations. How do I do this? Here are my three files below:
A.h:
#pragma once
class A
{
private:
A();
};
B.h
#pragma once
class A;
class B
{
public:
A createA();
};
B.cpp:
#include "A.h"
#include "B.h"
A B::createA()
{
A result; //cannot access private member declare in class 'A'
return result;
}
I've tried adding this to A.h:
public: friend A createA();
I've instead tried adding this to A.h with a corresponding forward declaration:
public: friend A B::createA();
I've instead tried adding and extern class B; to A.h and making B a class like this:
public: friend class B;
I'm at a loss.
I think this might be easier if I have the B::createA() function return a pointer to an A object rather than an A object directly, but that won't do in my case. I am emulating a closed API and the API call returns an A object rather than a pointer.
You probably just need to drop the "extern" from your third attempt to turn it into a proper forward-declaration. Try:
A.h:
#pragma once
class B;
class A
{
friend class B;
private:
A();
};
You don't need the external keyword. Make it simple:
// In A.h
class B; // Forward declaration
class A
{
friend class B; // Make all the class B friend
A();
};
// In B.h
class B
{
public:
A createA() {}
};
Live Example.
Unless absolutely necessary, you should have A construct itself (or have a factory that creates A). If you really want B to do it:
class B; // foward declared
class A
{
private:
A() {}
friend class B;
};
class B
{
public:
A CreateA()
{
A a;
return a;
}
};
int main()
{
B b;
A a = b.CreateA();
return 0;
}
Note: You must forward declare B before declaring it a friend in A.
If you want just the function as a friend:
class A;
class B
{
public:
A CreateA();
};
class A
{
private:
A() {}
friend class A B::CreateA();
};
A B::CreateA()
{
A a;
return a;
}
int main()
{
B b;
A a = b.CreateA();
return 0;
}
You can make B a friend of A:
class A
{
private:
A();
friend class B;
};

incomplete type error , when using friend functions

#include <stdio.h>
class B;
class A;
class A
{
int a;
friend int B::f();
};
class B
{
int b;
class A x;
public:
int f();
};
int B::f()
{
// ...
}
main()
{
class B b;
b.f();
}
ERRORS:
a.cpp:9: error: invalid use of incomplete type ‘struct B’
a.cpp:2: error: forward declaration of ‘struct B’
The issue cannot be solved by placing definition of B before A as
B has an object of type A.
For this example making B a friend class would do, but in
my real code I have more member functions in B (so I need alternative solution).
Finally, can somebody give me links that explain what the compiler does when it comes
across a forward declaration, declaration, definition.
You simply cannot do what you want to do as-is. To make that friend function declaration in class A the nature of class B needs to be known prior to the definition of class A. To make class B contain an instance of class A the nature of class A must be known prior to the definition of class B. Catch-22.
The former does not apply if you make class B a friend class of class A. The latter does not apply if you modify B to contain a pointer or reference to an instance of class A.
DefineB before A, and declare a pointer to A as member data of B:
class A; //forward declaration
class B
{
int b;
A *px; //one change here - make it pointer to A
public:
int f();
};
class A
{
int a;
friend int B::f();
};
Or, you could make the entire class B a friend of A, that way you don't have to make the member data pointer to A.
class B; //forward declaration
class A
{
int a;
friend class B;
};
class B
{
int b;
A x; //No change here
public:
int f();
};
Forward declare class A; define class B; define class A; define B::f.
#include <cstdio>
class A;
class B
{
int b;
public:
int f();
};
class A
{
int a;
friend int B::f();
};
int B::f()
{
}
main()
{
class B b;
b.f();
}

mutually referential classes yield "incomplete type" error

I have a situation in which A has a reference to a class C defined inside B, and C has an instance of class B.
When I try to compile the code below, I get "field a has incomplete type". I assume this is because the compiler does not know how much memory it should allocate for an instance of A.
class A;
class B {
public:
class C {
A a;
};
};
class A {
A(const B::C& _c)
: c(_c)
{}
const B::C& c;
};
But when I try to compile this, I get "C in class B does not name a type":
class B;
class B::C;
class A {
A(const B::C& _c)
: c(_c)
{}
const B::C& c;
};
class B {
public:
class C {
A a;
};
};
How can I convince the compiler that B::C is a real type?
As an absolute guess, I notice there's one permutation you haven't tried:
class B {
public:
class C; // Forward declaration
};
class A {
A(const B::C& _c)
: c(_c)
{}
const B::C& c;
};
class B::C {
A a;
C() : a(*this) {} // Thanks Nim for pointing this out!
};
This is quite possibly illegal, but worth a shot I think. If it doesn't work, then I don't see any way around the problem.
The forward declaration for A doesn't serve a purpose: you can't declare an instance of an incomplete type.
As to B::C, I don't think you can use nested names in an incomplete type. Just don't nest C in B: as far as I know this doesn't give you any significant advantages* and stops you from forward declaring it.
*The only advantage I can think of is that you can define it in the private section, but then A would have no business with it in the first place.

C++ - 2 classes 1 file

Suppose I want something of this sort, in one .cpp source file:
class A {
public:
void doSomething(B *b) {};
};
class B {
public:
void doSomething(A *a) {};
};
Is there anyway of doing this without splitting it into two separate files, and without receiving a compiler error (syntax error on doSomething(B *b))
put at the first line:
class B;
If I remember well, you can 'pre-declare' your class B.
class B; // predeclaration of class B
class A
{
public:
void doSomething(B* b);
}
class B
{
public
void doSomething(A* a) {}
}
public void A::doSomething(B* b) {}
Then, your class 'A' knows that a class 'B' will exists, although it hasn't been really defined yet.
Forward declaration is indeed the correct term, as mentionned by Evan Teran in the comments.
forward declare one class before other with
class B;
or
class A;
But still you won't be able to implement
void doSomething(B *b)
using only forward declaration of B. So you have to put definition of doSomething below full class A declaration
Yes. You need a forward declaration:
class B; // add this line before A's declaration
class A {
public:
void doSomething(B *b) {};
};
class B {
public:
void doSomething(A *a) {};
};
The C++ FAQ Lite answers this question and others. I'd seriously considering reading that thing end to end, or getting the book and doing the same.
You can try a forward declaration like
class B;
class A {
void Method( B* );
};
class B{
};
but you will only be able to declare pointer and reference variables for B then. If you want more (like a method that dereferences B* variable) you can provide a declaration only and define methods later in the same file - at the point where both classes declaration is already available.
You need to forward declare B.
class B;
class A
{
public:
void doSomething(B *b) {}
};
class B
{
public:
void doSomething(A *a) {}
};
(And BTW, you don't need the semi-colons after the member function curly braces. :) )
Add another declaration of B before A:
class B;
class A {
public:
void doSomething(B *b) {};
};
class B {
public:
void doSomething(A *a) {};
};