| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1777 人关注过本帖
标题:哪里错了,求解,运算符重载
只看楼主 加入收藏
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
哪里错了,求解,运算符重载
编译通不过
#include <iostream>
using namespace std;
class base
{
    float real,image;
    public:
        base (){}
        base (float x,float y):real(x),image(y){
        }
         base operator + (base ,base );
        void show()
        {
            cout<<real<<" +"<<image<<"i"<<endl;
        }
};

 base base::operator+(base a,base b)
 {
     base c;
     c.real=a.real+b.real;
     c.image=a.image+b.image;
     return c;
 }
 
 main()
 {
     base a(1.2,4.5),b(2.2,5.5);
     base c;
     c=a+b;
     c.show();
 }

[此贴子已经被作者于2019-8-15 19:52编辑过]

搜索更多相关主题的帖子: 运算符重载 base float real image 
2019-08-15 19:03
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:20 
base base::operator+(base a,base b)
要么去掉一个参数,变为 base base::operator+( base rhs ),即 *this 和 rhs 相加
要么加上friend,变为 friend base base::operator+(base a,base b),即与 *this 没有任何关系

程序代码:
#include <iostream>

class base
{
public:
    base() : real_(), image_()
    {
    }
    base( double real, double image=0.0 ) : real_(real), image_(image)
    {
    }
    friend base operator+( const base& lhs, const base& rhs );
    friend std::ostream& operator<<( std::ostream& os, const base& val );

protected:
    double real_, image_;
};

#include <iomanip>
#include <sstream>
using namespace std;

base operator+( const base& lhs, const base& rhs )
{
    return base( lhs.real_+rhs.real_, lhs.image_+rhs.image_ );
}
std::ostream& operator<<( std::ostream& os, const base& val )
{
    std::ostringstream buf;
    buf.flags( os.flags() );
    buf.imbue( os.getloc() );
    buf.precision( os.precision() );
    buf << val.real_ << setiosflags(ios::showpos) << val.image_ << 'i';
    return os << buf.str();
}

int main( void )
{
    base a(1.2,4.5), b(2.2,5.5);
    base c = a + b;
    cout << c << endl;

    cout << base(-1,-2) << endl;
}

2019-08-16 09:07
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
哦,知道了,谢谢!
2019-08-16 11:28
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#include <iostream>
#include <iomanip>
using namespace std;
class base
{
    float real,image;
    public:
        base (){}
        base (float x,float y):real(x),image(y){
        }
          base operator + (base);
        void show()
        {
            cout<<real<<" +"<<image<<"i"<<endl;
        }
        private:
            
};
 base base::operator+(base a)
 {
    base c;
     c.real=real+a.real;
     c.image=image+a.image;
     return c;
 }
 
 main()
 {
     base a(1.2,4.5),b(2.2,5.5);
     base sum;
     sum=a.operator +(b);
     sum.show();
 }
2023-09-20 12:22
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#include <iostream>
using namespace std;

class time  {
        int hours,minutes;
    public:
        time() {

        }
        time(int a,int b){
            hours=a,minutes=b;
        }
        ~time() {

        }
        time operator+(time & );
       void    show();
};
time time::operator+(time &t) {
        time sum;
        sum.minutes=minutes+t.minutes;
        sum.hours=hours+t.hours+sum.minutes/60;
        sum.minutes%=60;
        return sum;
        }
void    time::show(){
           cout<<"总共是"<<hours<<"小时"<<minutes<<"分钟"<<endl;
       }        
int main()
{
    time a(31,35),b(21,36);
    time sum;
    sum=a.operator+(b);//sum=a+b也可以
    sum.show();
}


[此贴子已经被作者于2023-9-21 13:24编辑过]

2023-09-21 11:16
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#include <iostream>
using namespace std;

class time  {
        int hours,minutes;
    public:
        time() {

        }
        time(int a,int b){
            hours=a,minutes=b;
        }
        ~time() {

        }
        time total (time &,time & );
       void    show();
};
time time::total (time &t1,time &t2) {
        time sum;
        sum.minutes=t1.minutes+t2.minutes;
        sum.hours=t1.hours+t2.hours+sum.minutes/60;
        sum.minutes%=60;
        return sum;
        }
void    time::show(){
           cout<<"总共是"<<hours<<"小时"<<minutes<<"分钟"<<endl;
       }        
int main()
{
    time a(31,37),b(21,36);
    time sum;
    sum=sum.total(a,b);
    sum.show();
}
2023-09-21 12:41
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
#include <iostream>
using namespace std;

class time  {
        int hours,minutes;
    public:
        time() {

        }
        time(int a,int b){
            hours=a,minutes=b;
        }
        ~time() {

        }
        time operator+(time & );
        time operator*(int);
      friend  time operator*(int a,time t){ //友元函数
            return t*a;
        }
       void    show();
};
time time::operator+(time &t) {
        time sum;
        sum.minutes=minutes+t.minutes;
        sum.hours=hours+t.hours+sum.minutes/60;
        sum.minutes%=60;
        return sum;
        }
        time time::operator*(int a) {
                time sum;
                sum.minutes=minutes*a+hours*a*60;
                sum.hours=sum.minutes/60;
                sum.minutes%=60;
                return sum;
                }
void    time::show(){
           cout<<"总共是"<<hours<<"小时"<<minutes<<"分钟"<<endl;
       }        
int main()
{
    time a(31,35),b(21,36);
    time sum;
    sum=2*a;
    sum.show();
    sum=b*2;
    sum.show();
}
2023-09-21 14:02
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1745
专家分:3216
注 册:2015-12-2
收藏
得分:0 
复数运算符重载
#include <iostream>
using namespace std;
class complex
{
    double real,image;
    friend complex operator+(complex,complex);
    public:
        complex(){};
        complex(double _real,double _image):real(_real),image(_image){}
    void show(){cout<<real<<"+"<<image<<"i"<<endl;}
};

complex operator+(complex a,complex b)
{
    complex c;
    c.real=a.real+b.real;
    c.image=a.image+b.image;
    return c;
}
int main()
{
    complex result,a(3,5.5),b(7.2,3.1);
    result=a+b;
    result.show();
}
2023-10-07 11:57
快速回复:哪里错了,求解,运算符重载
数据加载中...
 
   



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

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