| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 379 人关注过本帖
标题:一个小问题
收藏  订阅  推荐  打印 
hellodudu
Rank: 1
等级:新手上路
帖子:5
积分:150
注册:2006-7-2
一个小问题

#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
谢谢先~

搜索更多相关主题的帖子: weight  void  Boat  int  Car  
2006-10-3 18:59
song4
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:38
帖子:1506
积分:15212
注册:2006-3-25

注意大小写

我说自尊那 看起来或许可笑 但它至少支着我 试着不让我颠倒 活着 如果只是不甘寂静的喧嚣 那就咆哮吧 让每个人都听得到学习JAVA
2006-10-3 19:05
hellodudu
Rank: 1
等级:新手上路
帖子:5
积分:150
注册:2006-7-2

小写的car和boat是对象啊~
我还是看不出来哪的问题!

2006-10-3 19:17
song4
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:38
帖子:1506
积分:15212
注册:2006-3-25

一个友类是对这个类的
不是对这个对象的
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();
}


我说自尊那 看起来或许可笑 但它至少支着我 试着不让我颠倒 活着 如果只是不甘寂静的喧嚣 那就咆哮吧 让每个人都听得到学习JAVA
2006-10-3 19:35
hellodudu
Rank: 1
等级:新手上路
帖子:5
积分:150
注册:2006-7-2

但我定义的只是一个友元函数totalWeight.友元函数可以通过对象访问私有数据的吧.
2006-10-3 19:46
song4
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:38
帖子:1506
积分:15212
注册:2006-3-25

你把那俩类用参数传进去
就好了

我说自尊那 看起来或许可笑 但它至少支着我 试着不让我颠倒 活着 如果只是不甘寂静的喧嚣 那就咆哮吧 让每个人都听得到学习JAVA
2006-10-3 19:46
hellodudu
Rank: 1
等级:新手上路
帖子:5
积分:150
注册:2006-7-2

我试试
2006-10-3 19:59
hellodudu
Rank: 1
等级:新手上路
帖子:5
积分:150
注册:2006-7-2

我改了,还是不行
我刚开始学,对这方面的语法还不太熟,能请你说的详细点吗?

#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);
}

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

2006-10-3 20:27
冷血无情
Rank: 1
等级:新手上路
帖子:29
积分:390
注册:2006-9-25

一个友元函数只能访问它所属类的私有成员,不能访问其它类的私有成员,

2006-10-3 23:59
comebaby
Rank: 2
等级:注册会员
帖子:85
积分:956
注册:2006-6-17

#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;
}

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

2006-10-5 21:19
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.081361 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved