本文介绍了在C ++中应该声明哪些类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有这个C ++代码:
Say I got this C++ code:
class class1{
class2 *x;
}
class class2{
class1 *x;
}
编译器会在第2行给出错误,因为它无法找到class2 ,和相同,如果我切换类的顺序。如何解决这个问题?
The compiler would give an error in line 2 because it couldn't find class2, and the same if i switched the order of the classes. How do I solve this?
推荐答案
有两件事 - 一类,在类声明后需要分号:
Two things - one, you need semicolons after class declarations:
class class1{
class2 *x;
};
class class2{
class1 *x;
};
二,可以在类的定义之前创建一个声明。这告诉编译器这个类存在,你还没有定义它。在这种情况下,在 class1
的定义前放置 class2
声明:
Two, you can create a declaration in front of the definitions of the classes. That tells the compiler that this class exists, and you have yet to define it. In this case, put a class2
declaration in front of the definition of class1
:
class class2 ;
class class1{
class2 *x;
};
class class2{
class1 *x;
};
这篇关于在C ++中应该声明哪些类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!