| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:关于c++友元类的错误
只看楼主 加入收藏
wghost
Rank: 2
等 级:论坛游民
帖 子:47
专家分:31
注 册:2009-6-6
结帖率:37.5%
收藏
已结贴  问题点数:16 回复次数:2 
关于c++友元类的错误
//Time.h
class Date;
class Time  
{
public:
    Time(int,int,int );
    void display(const Date&);
    virtual ~Time();
private:
    int hour;
    int minute;
    int sec;

};
//Date.h
class Time;
class Date  
{    friend  class Time;
public:
    Date(int,int,int);

    virtual ~Date();
private:
    int month;
    int day;
    int year;
};
//Time.cpp
Time::Time(int h,int m,int s)
{
 hour=h;
 minute=m;
 sec=s;
}

Time::~Time()
{

}
void display( const Date & d)
{
    cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
    cout<<hour<<"/"<<minute<<"/"<<sec<<endl;
}
//Date.cpp
Date::Date(int m,int d,int y)
{
 month=m;
 day=d;
 year=y;
}

Date::~Date()
{

}
//主函数
#include"Date.h"
#include"Time.h"
int main()
{
    Time t1(10,13,56);
    Date d1(12,15,2004);
    t1.display(d1);
    return 0;
}
我用的是vc++环境为什么总提示下列错误啊,各位高手多指教啊
Compiling...
Time.cpp
D:\c++\ex3_h11\Time.cpp(26) : error C2248: 'month' : cannot access private member declared in class 'Date'
        d:\c++\ex3_h11\date.h(21) : see declaration of 'month'
D:\c++\ex3_h11\Time.cpp(26) : error C2248: 'day' : cannot access private member declared in class 'Date'
        d:\c++\ex3_h11\date.h(22) : see declaration of 'day'
D:\c++\ex3_h11\Time.cpp(26) : error C2248: 'year' : cannot access private member declared in class 'Date'
        d:\c++\ex3_h11\date.h(23) : see declaration of 'year'
D:\c++\ex3_h11\Time.cpp(27) : error C2065: 'hour' : undeclared identifier
D:\c++\ex3_h11\Time.cpp(27) : error C2065: 'minute' : undeclared identifier
D:\c++\ex3_h11\Time.cpp(27) : error C2065: 'sec' : undeclared identifier
Error executing cl.exe.

搜索更多相关主题的帖子: private display public friend minute 
2009-10-31 20:23
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:16 
void display( const Date & d)
这个应该写成下面这样
void Time::display( const Date & d)

要小心细节啊

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-10-31 22:19
wghost
Rank: 2
等 级:论坛游民
帖 子:47
专家分:31
注 册:2009-6-6
收藏
得分:0 
谢啦!!
2009-11-04 10:33
快速回复:关于c++友元类的错误
数据加载中...
 
   



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

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