不完全类型错误 [英] incomplete type error
问题描述
我想让A类成为B类的朋友。
class A {
public:
void show(const B&); // ## 1 ## but this one work fine
B ob; //错误不完整类型
};
class B {
public:
int b;
B():b(1){}
friend class A;
};
所以我的问题为什么是不完整的类型?我认为当我做 B类
它就像一个函数的原型,告诉编译在代码中的某处有一个定义。
也可以在上面的代码中使用`b
$ b
>不,这是一个向前的声明,并没有定义一个完整的类型。如果你想保持成员作为一个对象,你需要在 A
之前有一个完整的定义 B
而不是指针。
其中一个原因是必须知道类 B
的大小 A
,因为 A
的大小取决于 B
。
我建议你在 Ah
#includeBh
>。
编辑:澄清:
struct A;
struct B
{
A foo();
void foo(A);
void foo(A&);
void foo(A *);
A * _a;
A& __一个;
A a; //< --- only error here
};
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
};
这篇关于不完全类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!