注册 登录
编程论坛 C++教室

运算符重载困惑

kittel 发布于 2010-11-30 10:29, 1814 次点击
我照着书上写了个类,其中用到了输出流重载,跟书上写的一样,但运行起来就是有错误,不知道是我的程序写的有问题还是因为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;
    }
23 回复
#2
zhoufeng19882010-11-30 11:01
我使用g++编译器,运行没有问题。
#3
xanxus2010-11-30 11:08
删掉 cout<<daa;
这句话C++识别不了。
#4
kittel2010-11-30 12:46
回复 3楼 xanxus
删去就失去我本来的意义了,我本来就是想用重载的输出流操作。
#5
laoyang1032010-11-30 15:35
程序代码:
#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();
        ostream& operator<<(ostream& out);
    };
    void Date::set(int y,int m,int d)
    {
        year=y;month=m;day=d;
    }
    ostream& Date::operator<<(ostream& out)
      {
          out<<year<<"-"<<month<<"-"<<"-"<<day<<endl;
          return out;
      }
    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);
        daa<<cout;
        return 0;
    }
无能为力   我也 找不出那个怎么就二义性了   帮你写成了重载为类的成员函数  
可以运行   抱歉恕我无能
#6
pangding2010-11-30 18:02
我编译也没问题語法错误(只有一个警告)。它报哪行的 << 有歧义?
#7
kittel2010-11-30 20:08
回复 6楼 pangding
最后main中的输出符号<<模拟不可。你用的是VC吗?
#8
kittel2010-11-30 20:15
回复 6楼 pangding
还有就是为什么在VC中类的私有数据成员不能访问,但书上可以?
#9
平常心飞儿2010-11-30 22:01
应该是你的开发工具的问题   我试了  没有错误 你把提错的地方标出来
#10
pangding2010-11-30 22:27
回复 8楼 kittel
我用的 g++,不过还真不是很清楚你这个为什么会歧义。那个错误的完整描述是什么?它有没有说是怎么个歧义法?有时错误会显示好几行,跟着的几个都在指示一个错误。

另外,本来私有成员外界就不能访问。你的书上是怎么说的?你给个示例程序我们来分析一下。
#11
kittel2010-12-01 09:25
回复 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)
但是钱能的书上这种对私有数据成员都是可以直接访问的。。
#12
qkhhss2010-12-01 09:55
没问题啊,我运行可以哪,连警告都有没有.
#13
pangding2010-12-01 16:45
回复 11楼 kittel
诡异。我也觉得是你编译器的问题……
#14
ml2325282010-12-03 21:36
不肯能有问题吧 是你们编译器太弱了?
#15
ml2325282010-12-03 21:43
回复 11楼 kittel
他是朋友 私有数据怎么可能有问题
你的编译器太弱了
#16
li_danwang2010-12-03 21:49
#include<iostream>
using namespace std;


你把这个改成,
#include<iostream.h>就对了..
#17
ml2325282010-12-03 22:02
回复 10楼 pangding
朋友可以访问私有数据
程序代码:



#include<iostream>
using namespace std;

class abc
{
private:
    int a;
protected:
    int b;
    friend int geta(abc&);
};
int geta(abc& it)
{
    return it.a;
}
class abcd:public abc
{
public:
    int show(){return b;}
};
int main()
{
    abcd abcd1;
    cout<<abcd1.show();
    abc abc1;
    cout<<geta(abc1);
    getchar();
    return 1;
}




能运行吗

#18
cadypeng2010-12-04 14:18
回复 楼主 kittel
应该是你编译器问题。在vc6下可以运行。私有成员数据在有元函数是可以访问
#19
cadypeng2010-12-04 14:27
回复 16楼 li_danwang
普通函数设置了友元函数当然是可以访问私有数据成员的
#20
kittel2010-12-04 20:23
回复 17楼 ml232528
能。
#21
mengxin33102010-12-08 14:09
把头两行改为#include<iostream.h>
#22
mengxin33102010-12-08 14:10
程序运行正确
#23
尝鲜2010-12-08 14:36
#include<iostream>
    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 std::ostream& operator<<(std::ostream&,const Date&);
    };
    using namespace std;
    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;
    }
#24
kittel2010-12-09 19:02
回复 23楼 尝鲜
using namespace std;为什么要放后面?
1