| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1089 人关注过本帖
标题:一个小问题
只看楼主 加入收藏
hellodudu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-2
收藏
 问题点数:0 回复次数:12 
一个小问题

#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-03 18:59
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
注意大小写

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-10-03 19:05
hellodudu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-2
收藏
得分:0 

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

2006-10-03 19:17
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 

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


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-10-03 19:35
hellodudu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-2
收藏
得分:0 
但我定义的只是一个友元函数totalWeight.友元函数可以通过对象访问私有数据的吧.
2006-10-03 19:46
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
你把那俩类用参数传进去
就好了

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-10-03 19:46
hellodudu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-2
收藏
得分:0 
我试试
2006-10-03 19:59
hellodudu
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-2
收藏
得分:0 

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

#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-03 20:27
冷血无情
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2006-9-25
收藏
得分:0 
一个友元函数只能访问它所属类的私有成员,不能访问其它类的私有成员,

2006-10-03 23:59
comebaby
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-6-17
收藏
得分:0 

#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-05 21:19
快速回复:一个小问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013677 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved