error C2027: use of undefined type 'A' see declaration of 'A'

来源:互联网 发布:万国数据科技公司电话 编辑:程序博客网 时间:2024/05/17 01:00
#include<iostream>using namespace std;class A;class B{public :B(A * a){a->c = 5;}};class A{public:int c;void set(){B b(this);cout<<c;}};void main(){A a;a.set();}/*error C2027: use of undefined type 'A'see declaration of 'A'error C2227: left of '->c' must point to class/struct/union

*/

在定义类B之前,只是声明了类A,这个声明是前向声明,在类A声明之后,定义之前,类A 是不完整类型,即已知A是一个类型,但不知道包含哪些成员。对于不完整类型,只能定义一个A类型的指针或引用,且不能访问它的成员,因为编译器只知道有类A存在而不知道类A到底是什么。

0 0
原创粉丝点击