| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 597 人关注过本帖
标题:C++ 运算符重载的问题
取消只看楼主 加入收藏
EndingWu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2014-4-6
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
C++ 运算符重载的问题
以下代码不知问题处于何处 在vc++6.0上可以通过 但是在学校的online judge 上就过不了 出现编译错误
#include <iostream>
#include <iomanip>
using namespace std;
class RMB;
RMB operator -(RMB& ,RMB&);
RMB operator --(RMB& );
  
class RMB
{
public:
    RMB(int ,int ,int );
    void initial(int ,int ,int);
    void display(ostream&);
    RMB operator ++(int);
    RMB operator +(RMB& );
    friend RMB operator --(RMB& );
    friend RMB operator -(RMB& ,RMB&);
protected:
    int Yuan;
    int Jiao;
    int Fen;
};
  
RMB::RMB(int a,int b,int c)
{
    Yuan=a;
    Jiao=b;
    Fen=c;
}
RMB RMB::operator ++(int)
{
    Fen++;
    if(Fen>=10)
    {
        Fen-=10;
        Jiao++;
    }
    if(Jiao>=10)
    {
        Jiao-=10;
        Yuan++;
    }
    return *this;
}
RMB RMB::operator +(RMB& m)
{
    int fen,jiao,yuan;
    fen=Fen+m.Fen;
    jiao=Jiao+m.Jiao;
    yuan=Yuan+m.Yuan;
    if(fen>=10)
    {
        fen-=10;
        jiao++;
    }
    if(jiao>=10)
    {
        jiao-=10;
        yuan++;
    }
    RMB result1(yuan,jiao,fen);
    return result1;
}
RMB operator --(RMB& m)
{
    m.Fen--;
    if(m.Fen<0)
    {
        m.Fen+=10;
        m.Jiao--;
    }
    if(m.Jiao<0)
    {
        m.Jiao+=10;
        m.Yuan--;
    }
    return m;
}
RMB operator -(RMB& m1,RMB& m2)
{
    int fen,jiao,yuan;
    fen=m1.Fen-m2.Fen;
    jiao=m1.Jiao-m2.Jiao;
    yuan=m1.Yuan-m2.Yuan;
    if(fen<0)
    {
        fen+=10;
        jiao--;
    }
    if(jiao<0)
    {
        jiao+=10;
        yuan--;
    }
    RMB result(yuan,jiao,fen);
    return result;
}
void RMB::initial(int yuan,int jiao,int fen)
{
    Yuan=yuan;
    Jiao=jiao;
    Fen=fen;
}
void RMB::display(ostream& out)
{
    out<<Yuan<<' '<<Jiao<<' '<<Fen<<endl;
}
ostream& operator <<(ostream& o,RMB& m)
{
    m.display(o);
    return o;
}
  
  
  
int main()
{
    RMB A(0,0,0);
    RMB B(0,0,0);
  
    int A1,A2,A3;
    int B1,B2,B3;
    int i=0;
  
    cin>>A1>>A2>>A3;
    cin>>B1>>B2>>B3;
  
    A.initial(A1,A2,A3);
    B.initial(B1,B2,B3);
  
    cout<<A+B;
    cout<<A-B;
    for(;i<5;i++)
    {
        A--;
        B++;
    }
    cout<<A;
    cout<<B;
  
    return 0;
}
 
编译错误:

Main.cc: In function 'int main()':
Main.cc:131: error: no match for 'operator<<' in 'std::cout << A.RMB::operator+(((RMB&)(& B)))'
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:127: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:165: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:169: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:173: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:91: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:180: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:105: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:191: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:200: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:209: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:213: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:221: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:225: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:119: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
Main.cc:108: note:                 std::ostream& operator<<(std::ostream&, RMB&)
Main.cc:132: error: no match for 'operator<<' in 'std::cout << operator-(((RMB&)(& A)), ((RMB&)(& B)))'
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:127: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:165: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:169: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:173: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:91: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:180: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:105: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:191: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:200: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:209: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:213: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:221: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:225: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:119: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
Main.cc:108: note:                 std::ostream& operator<<(std::ostream&, RMB&)
Main.cc:135: error: no 'operator--(int)' declared for postfix '--', trying prefix operator instead


以下是内容:
PDE运算符重载之人民币运算

Description
人民币包含元角分三个成员,包括操作:初始化、递增、递减、相加、相减、显示

递增,用运算符++表示,实际操作是加1分,用成员函数重载的方法(非友元)

递减,用运算符--表示,实际操作是减1分,用友元方法进行重载

相加,用运算符+表示,操作是两个人民币对象相加,分别对元角分三个成员相加,如果有进位还要高位加1低位减10,对+用成员函数重载的方法

相减,用运算符-表示,操作是两个人民币对象相减,分别对元角分三个成员相减,如果有借位还要高位减加1低位加10,对-用友元方法进行重载


初始化,就是对元角分进行设置

显示,用运算符<<表示,输出格式为:元-角-分,用友元方法或外部函数的方法进行重载都可以








Input

输入两行,每行输入一个人民币对象的元角分数值

Output
假定两个人民币对象为A\B

第一行输出A+B的结果,必须使用运算符+

第二行输出A-B的结果,必须使用运算符-

第三行输出A--5次的结果,必须使用运算符--

第四行输出B++5次的结果,必须使用运算符++

上述所有输出必须使用重载运算符<<
搜索更多相关主题的帖子: display include initial public online 
2014-06-26 15:23
EndingWu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2014-4-6
收藏
得分:0 
回复 2 楼 embed_xuel
求教……
2014-06-28 21:39
快速回复:C++ 运算符重载的问题
数据加载中...
 
   



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

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