首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这里为什么不是用引用或者指针?解决方案

2012-05-28 
这里为什么不是用引用或者指针?#include iostreamusing namespace stdclass Bclass Cclass A{public:

这里为什么不是用引用或者指针?
#include <iostream>
using namespace std;
class B;
class C;
class A{
  public:
  A(){
  }
  void f(C b){}//换成C&就没问题了。 why?
};
class B:public A{
};
class C{
};
int main()
{

}
test3.cc: In member function ‘void A::f(C)’:
test3.cc:9: 错误:‘b’ has incomplete type
test3.cc:4: 错误:forward declaration of ‘struct C’

[解决办法]
void f(C b){}需要知道C的具体类型,因为参数需要拷贝进去,而现在根本不知道C的大小,虽然你在下面写了C的定义,但是编译器编译这一行的时候看不见。所以编译不过。incomplete type。
如果参数是引用或者指针就没有问题,因为引用和指针都是编译时刻做简单类型检查,并不要求知道对象的定义

热点排行
Bad Request.