| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1814 人关注过本帖
标题:运算符重载困惑
取消只看楼主 加入收藏
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
运算符重载困惑
我照着书上写了个类,其中用到了输出流重载,跟书上写的一样,但运行起来就是有错误,不知道是我的程序写的有问题还是因为VC和borlandC的差异导致的。我用的是VC,书上用的是borlandC++。运行时提示,<<符是ambiguous模拟两可的。
#include<iostream>
using namespace std;
    class Date {
    public:
        int year,month,day;
    public:
        int yearo() {return year;}
        int montho() {return month;}
        int dayo() {return day;}
        void set(int,int,int);
        void add();
        bool isrun();
       friend ostream& operator<<(ostream&,const Date&);
    };
    void Date::set(int y,int m,int d) {
        year=y;month=m;day=d;}
      ostream& operator<< (ostream& out,const Date& da) {
        return out<<da.year<<"-"<<da.month<<"-"<<"-"<<da.day<<endl;}
    bool Date::isrun() {
        if(year%4==0 && year%100!=0 || year%400==0) return true;
        return false;}

    int main() {
        Date daa;
        int y,m,d;
        cout<<"please input the date "<<endl;
        cin>>y>>m>>d;
        daa.set(y,m,d);
        cout<<daa;
        return 0;
    }
搜索更多相关主题的帖子: 运算符 困惑 重载 
2010-11-30 10:29
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
收藏
得分:0 
回复 3楼 xanxus
删去就失去我本来的意义了,我本来就是想用重载的输出流操作。
2010-11-30 12:46
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
收藏
得分:0 
回复 6楼 pangding
最后main中的输出符号<<模拟不可。你用的是VC吗?
2010-11-30 20:08
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
收藏
得分:0 
回复 6楼 pangding
还有就是为什么在VC中类的私有数据成员不能访问,但书上可以?
2010-11-30 20:15
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
收藏
得分:0 
回复 10楼 pangding
Compiling...
f13.cpp
d:\c++admin\f13.cpp(29) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.

f13.obj - 1 error(s), 0 warning(s)
私有数据的访问就是说,比如我定义的类中,作如下更改,其他不变
class Date {
   
        int year,month,day;
    public:
        int yearo() {return year;}
        int montho() {return month;}
        int dayo() {return day;}
        void set(int,int,int);
        void add();
        bool isrun();
       friend ostream& operator<<(ostream&,const Date&);
    };
调试结果如下:
-------------------Configuration: f13 - Win32 Debug--------------------
Compiling...
f13.cpp
D:\c++admin\f13.cpp(18) : error C2248: 'year' : cannot access private member declared in class 'Date'
        D:\c++admin\f13.cpp(5) : see declaration of 'year'
D:\c++admin\f13.cpp(18) : error C2248: 'month' : cannot access private member declared in class 'Date'
        D:\c++admin\f13.cpp(5) : see declaration of 'month'
D:\c++admin\f13.cpp(18) : error C2248: 'day' : cannot access private member declared in class 'Date'
        D:\c++admin\f13.cpp(5) : see declaration of 'day'
D:\c++admin\f13.cpp(29) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.

f13.obj - 1 error(s), 0 warning(s)
但是钱能的书上这种对私有数据成员都是可以直接访问的。。
2010-12-01 09:25
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
收藏
得分:0 
回复 17楼 ml232528
能。
2010-12-04 20:23
kittel
Rank: 2
来 自:武汉
等 级:论坛游民
威 望:1
帖 子:38
专家分:73
注 册:2010-11-19
收藏
得分:0 
回复 23楼 尝鲜
using namespace std;为什么要放后面?
2010-12-09 19:02
快速回复:运算符重载困惑
数据加载中...
 
   



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

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