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

确实如此~

大小写出了问题~

难怪看了半天没找出来问题~
2009-10-08 13:12
快速回复:【求助】vc++菜鸟作业题求助~
数据加载中...
 
   



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

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