定义了两个类
//a.h
class A
{
...
B m_b;
...
};
//b.h
class B
{
..
};
在类A中为了声明类B,可以通过下面两种方式:
方法一:
a.h中 在开头写
class B;
a.cpp中
#include "b.h"
方法二:
a.h中添加一行 #include "b.h"
我在使用方法一时,编译不通过,只能按照方法二声明。哪位高手知道使用方法一与方法二应用环境的差别。
...全文
1446打赏收藏
在类中声明对象类型的问题
定义了两个类 //a.h class A { ... B m_b; ... }; //b.h class B { .. }; 在类A中为了声明类B,可以通过下面两种方式: 方法一: a.h中 在开头写 class B; a.cpp中 #include "b.h" 方法二: a.h中添加一行 #include "b.h" 我在使用方法一时,编译不通过,只能按照方法二声明。哪位高手知道使用方法一与方法二应用环境的差别。
class B{
int i;
public:
B(int j = 0):i(j){}
int getI(){return i;}
};
#endif
-------------multi.cc-------------//测试程序
#include <iostream>
#include "a.h"
using namespace std;
class A;
int main()
{
A m_a;
m_a.disp();
int i;
cin>>i;
return 0;
}
以上程序测试通过