n00b班级问题 [英] n00b class question

查看:61
本文介绍了n00b班级问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能在C类定义中创建A和B的实例?


#include< iostream>


B级;

A级;


C级{

公开:

B b;

A a;

};


B级{

int b; < br $>
};


A级{

int a;

};


int main(无效)

{

C c;


返回0;

}

Why can''t I create an instance of A and B in the Class C definition?

#include <iostream>

class B;
class A;

class C {
public:
B b;
A a;
};

class B {
int b;
};

class A {
int a;
};

int main (void)
{
C c;

return 0;
}

推荐答案


th ******** @ gmail.com 写道:

为什么我不能创建一个C类定义中A和B的实例?
Why can''t I create an instance of A and B in the Class C definition?



因为A类和B类尚未定义。

Because class A and B are not yet defined.


th ******** @ gmail.com 写道:

为什么我不能在C类定义中创建A和B的实例?


#include< iostream>


级B;

A级;


C级{

公开:

B b ;

A a;

};
Why can''t I create an instance of A and B in the Class C definition?

#include <iostream>

class B;
class A;

class C {
public:
B b;
A a;
};



要理解如何构造C的实例,编译器需要知道'B'的内部结构是什么,b $'和''A''。既然你没有定义(只声明)C之前的B和A类,编译器

不能做它需要的东西。移动''C''_ after_''B''和''A''定义的定义。

To understand how to construct an instance of C, the compiler needs to
know what the internal structures of ''B'' and ''A'' are. Since you didn''t
define (only declared) the classes ''B'' and ''A'' before ''C'', the compiler
cannot do what it needs. Move the definition of ''C'' _after_ ''B'' and ''A''
definitions.


>

B级{

int b;

};


A级{

int a;

};


int main(无效)

{

C c;


返回0;

}
>
class B {
int b;
};

class A {
int a;
};

int main (void)
{
C c;

return 0;
}



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要''请问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




Kavya写道:

Kavya wrote:
th ******** @ gmail.com 写道:

为什么可以'我在C类定义中创建A和B的实例?
Why can''t I create an instance of A and B in the Class C definition?



因为尚未定义A类和B类。


Because class A and B are not yet defined.



感谢您的回复,

我以为


B级;

A类;


一开始基本上帮助编译器知道

定义的其余部分将在以后发生?我怎么能知道A和B的

定义,同时仍然将它们列在C之后? (对于

更复杂的问题,其实质上意味着A包含B和B

包含A所以重新排序不会修复它。


AwooOOoo

Thanks for the reply,
I thought that the

class B;
class A;

at the start essentially helped the compiler know the rest of the
definition would be later on?How can I have it know about the
definition of A and B whilst still having them listed after C? (for a
more complicated problem which essentially means A contains B and B
contains A so reordering would not fix it.

AwooOOoo


这篇关于n00b班级问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆