找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 708|回复: 9

一个小问题

[复制链接]
发表于 2009-11-4 00:27:11 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include<iostream>
using namespace std;
class Boat
{
public:
Boat(int w=0) {weight=w;}
void coutB() {cout<<"Boat weight="<<weight<<endl;}
friend void totalWeight(); //定义totalweight为boat的友元函数
private:
int weight;
};
class Car
{
Car(int w=0) {weight=w;}
void coutB() {cout<<"Car weight="<<weight<<endl;}
friend void totalWeight(); //定义totalweight为car的友元函数
private:
int weight;
};
void totalWeight()
{
int B,C;
B=boat.weight;
C=car.weight; //通过对象访问其weight
cout<<"total weight="<<B+C<<endl;
}
void main()
{
Boat boat(120);
Car car(110); //生成对象
totalWeight();
}

实现功能:totalweight计算两者的重量之和
编译报错:error C2065: 'boat' : undeclared identifier
error C2228: left of '.weight' must have class/struct/union type
error C2065: 'car' : undeclared identifier
error C2228: left of '.weight' must have class/struct/union type
error C2248: 'Car::Car' : cannot access private member declared in class 'Car'
see declaration of 'Car::Car'



请高手帮忙!!1
谢谢先~
发表于 2009-11-4 00:27:12 | 显示全部楼层 IP:江苏扬州
注意大小写
回复

使用道具 举报

发表于 2009-11-4 00:27:13 | 显示全部楼层 IP:江苏扬州
小写的car和boat是对象啊~
我还是看不出来哪的问题!
回复

使用道具 举报

发表于 2009-11-4 00:27:14 | 显示全部楼层 IP:江苏扬州
一个友类是对这个类的
不是对这个对象的
void totalWeight()
{
int B,C;
B=boat.weight;
C=car.weight; //通过对象访问其weight
cout<<"total weight="<<B+C<<endl;
}
void main()
{
Boat boat(120);
Car car(110); //生成对象
totalWeight();
}
回复

使用道具 举报

发表于 2009-11-4 00:27:16 | 显示全部楼层 IP:江苏扬州
但我定义的只是一个友元函数totalWeight.友元函数可以通过对象访问私有数据的吧.
回复

使用道具 举报

发表于 2009-11-4 00:27:17 | 显示全部楼层 IP:江苏扬州
你把那俩类用参数传进去
就好了
回复

使用道具 举报

发表于 2009-11-4 00:27:18 | 显示全部楼层 IP:江苏扬州
我试试
回复

使用道具 举报

发表于 2009-11-4 00:27:19 | 显示全部楼层 IP:江苏扬州
我改了,还是不行
我刚开始学,对这方面的语法还不太熟,能请你说的详细点吗?

#include<iostream>
using namespace std;
class Boat
{
public:
Boat(int w=0) {weight=w;}
void coutB() {cout<<"Boat weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为boat的友元函数
private:
int weight;
};
class Car
{
Car(int w=0) {weight=w;}
void coutB() {cout<<"Car weight="<<weight<<endl;}
friend void totalWeight(Boat &a,Car &b); //定义totalweight为car的友元函数
private:
int weight;
};
void totalWeight(Boat &p1,Car &p2)
{
int x,y;
x=p1.weight;
y=p2.weight; //通过对象访问其weight
cout<<"total weight="<<x+y<<endl;
}
void main()
{
Boat boat(120);
Car car(110); //生成对象
totalWeight(boat,car);
}

还有一个问题,就是友元函数对类的访问只能通过参数传递吗?
回复

使用道具 举报

发表于 2009-11-4 00:27:21 | 显示全部楼层 IP:江苏扬州
一个友元函数只能访问它所属类的私有成员,不能访问其它类的私有成员,
回复

使用道具 举报

发表于 2009-11-4 00:27:22 | 显示全部楼层 IP:江苏扬州
#include<iostream>
using namespace std;
class Boat
{
private:
int weight;

public:
Boat(int w=0)
{
weight=w;
}
void coutB()
{
cout<<"Boat weight="<<weight<<endl;
}
friend int totalWeight(Boat &a);
};
int totalWeight(Boat &a)
{
int B;
return B=a.weight;
}

class Car
{
private:
int weight;

public:
Car(int w=0)
{
weight=w;
}
void coutB()
{
cout<<"Car weight="<<Car::weight<<endl;
}

friend int totalWeight1(Car &b);
};
int totalWeight1(Car &b)
{
int C;
return C=b.weight;
}
int main()
{
Boat boat(120);

Car car(110);

cout<<totalWeight(boat)+totalWeight1(car);

return 0;
}

这是我改了你程序的一些代码还有格式
用楼主你写的代码调不了``可能是我水平问题吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 21:34 , Processed in 0.186017 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表