| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 437 人关注过本帖
标题:求教一个源代码问题,谢谢高手!!!
只看楼主 加入收藏
c453413516
Rank: 1
等 级:新手上路
帖 子:16
专家分:4
注 册:2010-12-5
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:5 
求教一个源代码问题,谢谢高手!!!
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
class CDate{                                                        //日期类
public:
        CDate(int y=2008,int m=9,int d=10);                //带默认参数
        CDate operator +(const int i);                        //加运算重载
        friend ostream operator <<(ostream& output,CDate & date);        //友元
public:
    int year,month,day;
};
//day属性的加运算
CDate CDate:perator +(const int i)
{
        CDate cd;
        cd.year=year;
        cd.month=month;
        cd.day=day+i;
        return cd;
}
//流输出
ostream operator <<(ostream& output,CDate & date)
{
        string str;
        char ch[10];
        str.assign(itoa(date.year,ch,10));
        str.append("/");
        str.append(itoa(date.month,ch,10));
        str.append("/");
        str.append(itoa(date.day,ch,10));
        return output<<str<<endl;
}
CDate::CDate(int y,int m,int d)
{
        year=y;
        month=m;
        day=d;
}
int main(void)
{
        CDate date(1999,9,9);
        cout<<date;                        //重载的输出流
        date=date+10;                        //重载加运算
        cout<<date;                        //重载的输出流
        return 0;
}
但是在编译的同时提示有问题:
e:\第16章\ch16_9\ch16_9.cpp(43) : error C2593: 'operator <<' is ambiguous
e:\第16章\ch16_9\ch16_9.cpp(45) : error C2593: 'operator <<' is ambiguous
希望高手能够解答这到底是哪里有问题了?
搜索更多相关主题的帖子: 源代码 
2011-05-26 22:04
记叙、继续
Rank: 4
等 级:业余侠客
帖 子:56
专家分:226
注 册:2011-5-17
收藏
得分:0 
额,我以我学c语言这一个月的经验告诉你。兄弟得多检查
2011-05-27 03:28
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
不是说了43和45行出问题了吗
2011-05-27 08:14
wavewind
Rank: 3Rank: 3
来 自:浙江
等 级:论坛游侠
帖 子:34
专家分:101
注 册:2011-5-13
收藏
得分:0 
#include <stdafx.h>
#include <iostream>   //由于在类CDate中有定义成员函数,应改为:#include <iostream.h>
#include <string>     //并去掉using namespace std;语句,如果你是在vc中进行编译运行
using namespace std;
2011-05-27 08:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
只修改语法错误,逻辑错误我不管

程序代码:
#include <iostream>

class CDate
{
    friend std::ostream& operator<<( std::ostream& os, const CDate& date );
public:
    CDate( int y=2008, int m=9, int d=10 );
    CDate operator+( const int d ) const;
public:
    int year,month,day;
};

CDate::CDate( int y, int m, int d ) : year(y), month(m), day(d)
{
}

CDate CDate::operator+( int d ) const
{
    return CDate( year, month, day+d );
}

std::ostream& operator<<( std::ostream& os, const CDate& date )
{
    return os << date.year << '/' << date.month << '/' << date.day;
}

using namespace std;
int main()
{
    CDate date(1999,9,9);
    cout << date << endl;
    cout << date+10 << endl;

    return 0;
}

2011-05-27 12:08
c453413516
Rank: 1
等 级:新手上路
帖 子:16
专家分:4
注 册:2010-12-5
收藏
得分:0 
感谢4楼得回答,已经解决了问题!!!
2011-05-27 14:21
快速回复:求教一个源代码问题,谢谢高手!!!
数据加载中...
 
   



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

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