不完全类型错误

编程入门 行业动态 更新时间:2024-10-23 20:31:11
本文介绍了不完全类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想让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 };

更多推荐

不完全类型错误

本文发布于:2023-11-27 08:56:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637385.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不完全   错误   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!