c++学习2011-02-27

背景:
友元 前向声明

 

源代码:
#include <iostream>
using namespace std;

//class Car;

class Boat
{
private:
 float Weight; 
public:
 Boat(float weight = 0)
 {
  Weight = weight;
 }
 friend float totalWeight(Boat &a,Car &b);
};


class Car
{
private:
 float Weight;
public:
 Car(float weight = 0)
 {
  Weight = weight;
 }
 friend float totalWeight(Boat &a,Car &b); 
};

 

 

float totalWeight(Boat &a,Car &b)
{
 float c;
 c = a.Weight + b.Weight;
 return c; 
}

 

int main()
{
 Boat aa(100.1);
 Car bb(200.2);
  cout << totalWeight(aa,bb) << endl;
 return 0;
}

 

错误提示:
syntax error : identifier 'Car' //语法错误:标识符Car
'Weight' : cannot access private member declared in class 'Boat' //不能在类Boat中公然的访问私有成员


错误分析:
Boat类和Cat类互相引用


研究结果(知识扩展):
我在开始时没有加上前向声明class Car,所以导致错误。
这个错误能在编译阶段查出来,是静态错误,语法错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值