| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1093 人关注过本帖
标题:【求助】vc++菜鸟作业题求助~
只看楼主 加入收藏
月光疾风
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2009-2-15
结帖率:100%
收藏
 问题点数:0 回复次数:5 
【求助】vc++菜鸟作业题求助~

目的是使用友元函数的方法求船和车重量的和

#include "iostream.h"
class Boat;
class Car{
friend int totalWeight(Boat &a,Car &b);
public:

    int GetNumber()
    {
        cin >> Number;
        return Number;
    }
    int GetWeight()
    {
        cin >> Weight;
        return Weight;
    }
     
private:
    int Number,Weight;
};
class Boat{
friend int totalWeight(Boat &a,Car &b);
public:
    int GetNumber()
    {
        cin >> Number;
        return Number;
    }
    int GetWeight()
    {
        cin >> Weight;
        return Weight;
    }
 
     
private:
    int Number,Weight;
};
int totalweight(Boat& a,Car& b)
{
 
    return a.Weight+b.Weight;
}
int main()
{
    Boat a;
    Car b;
    int x,y,z;
    x = a.GetWeight();
    y = b.GetWeight();
    z = totalweight(a,b);
    cout << z << endl;
    return 0;
}

结果显示错误如下:

--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
F:\New Folder (3)\5141\1.cpp(41) : error C2248: 'Weight' : cannot access private member declared in class 'Boat'
        F:\New Folder (3)\5141\1.cpp(36) : see declaration of 'Weight'
F:\New Folder (3)\5141\1.cpp(41) : error C2248: 'Weight' : cannot access private member declared in class 'Car'
        F:\New Folder (3)\5141\1.cpp(19) : see declaration of 'Weight'
Error executing cl.exe.

1.exe - 2 error(s), 0 warning(s)
 
搜索更多相关主题的帖子: 作业 
2009-10-07 13:41
a2696026
Rank: 2
等 级:论坛游民
帖 子:21
专家分:33
注 册:2009-9-9
收藏
得分:0 
以下是引用月光疾风在2009-10-7 13:41:46的发言:


目的是使用友元函数的方法求船和车重量的和

#include "iostream.h"
class Boat;
class Car{
friend int totalWeight(Boat &a,Car &b);
public:

    int GetNumber()
    {
        cin >> Number;
        return ...
试试去掉 class Boat
2009-10-07 14:29
月光疾风
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2009-2-15
收藏
得分:0 
回复 2楼 a2696026
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
F:\New Folder (3)\5141\1.cpp(3) : error C2061: syntax error : identifier 'Boat'
F:\New Folder (3)\5141\1.cpp(40) : error C2248: 'Weight' : cannot access private member declared in class 'Boat'
        F:\New Folder (3)\5141\1.cpp(35) : see declaration of 'Weight'
F:\New Folder (3)\5141\1.cpp(40) : error C2248: 'Weight' : cannot access private member declared in class 'Car'
        F:\New Folder (3)\5141\1.cpp(18) : see declaration of 'Weight'
Error executing cl.exe.

1.obj - 3 error(s), 0 warning(s)


缺少声明了
2009-10-07 14:41
haitao9999
Rank: 2
等 级:论坛游民
帖 子:30
专家分:39
注 册:2006-4-10
收藏
得分:0 
我修改了下,如下可以正常运行。你的友元函数定义和声明的不一致。在类内的声明是totalWeight(Boat &a,Car &b),而后边的定义和使用都是totalweight(Boat& a,Car& b) ,当然是不行的。声明为友元的函数未被调用,而调用的函数没有被声明为友元函数。所以报了错误提示不能访问类的私有成员。

#include <iostream>
using namespace std;
 
class Boat;
class Car{
friend int totalWeight(Boat &a,Car &b);
public:
    int GetNumber()
    {
        cin >> Number;
        return Number;
    }
    int GetWeight()
    {
        cout<<"enter the weight of car:";
        cin >> Weight;
        return Weight;
    }
      
private:
    int Number;
    int Weight;
};
class Boat{
friend int totalWeight(Boat &a,Car &b);
public:
    int GetNumber()
    {
        cin >> Number;
        return Number;
    }
    int GetWeight()
    {
        cout<<"enter the weight of boat:";
        cin >> Weight;
        return Weight;
    }
 
      
private:
    int Number,Weight;
};
 
int totalWeight(Boat &a,Car &b){
    return a.Weight+b.Weight;
}
int main()
{
    Boat a;
    Car b;
    int x,y,z;
    x = a.GetWeight();
    y = b.GetWeight();
    z = totalWeight(a,b);
    cout << z << endl;
    return 0;
}
2009-10-07 19:56
月光疾风
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2009-2-15
收藏
得分:0 
回复 4楼 haitao9999
万分感谢~

确实如此~

大小写出了问题~

难怪看了半天没找出来问题~
2009-10-08 13:12
a2696026
Rank: 2
等 级:论坛游民
帖 子:21
专家分:33
注 册:2009-9-9
收藏
得分:0 
回复 5楼 月光疾风
问下  你用的是什么样的编程辅助工具  a2696026@  与好的给我个
2009-10-13 14:50
快速回复:【求助】vc++菜鸟作业题求助~
数据加载中...
 
   



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

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