| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1641 人关注过本帖
标题:运算符重载的问题 不知道错在了哪里
只看楼主 加入收藏
柚夏怪
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-9-7
结帖率:25%
收藏
 问题点数:0 回复次数:4 
运算符重载的问题 不知道错在了哪里
class Compelx
{
  friend Complex operator+(double a);
  friend Complex operator-(double a);
  friend Complex operator-(const Complex &c);
  double r;
  double i;
public:
  Complex(double a,double b) : r(a),i(b){}
  Complex operator+(const Complex &c) const
  {
            return Complex(r+c.r,i+c.i);   
  }
  Complex operator-(const Complex &c) const
  {
    return Complex(r-c.r,i-c.i);
  }
  Complex operator+(double a) const
  {
    return Complex(r+a,i);
  }
  Complex operator-(double a) const
  {
    return Complex(r-a,i);
  }
};
Complex operator+(double a)
{
  return Complex(a+r,i);
}
Complex operator-(double a)
{
  return Complex(a-r,-i);
}
Complex operator-(const Complex &c)
{
  return Complex(-c.r,-c.i);
}
int main()
{
  Complex c1(5.6,4.5);
  Complex c2(2.3,3.3);
  Complex c1 + c2;
  Complex c1 - c2;
  double a = 2.3;
  Complex c1 + a;
  Complex c1 - a;
  operator+(a,c1);
  operator-(a,c2);
  operator-();
}
搜索更多相关主题的帖子: Complex public double friend return 
2016-11-12 09:37
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:0 
程序代码:
#include <iostream>
using namespace std;

class Compelx
{
public:
    explicit Compelx(const double& a, const double& b)
    {
        this->r = a;
        this->i = b;
    }

    virtual ~Compelx(void)
    {

    }

    Compelx& operator+(const double& a)
    {
        this->r += a;

        return (*this);
    }

    Compelx& operator-(const double& a)
    {
        this->r -= a;

        return (*this);
    }

    Compelx& operator-(const Compelx& c)
    {
        this->r -= c.r;
        this->i -= c.i;

        return (*this);
    }

    Compelx& operator+(const Compelx& c)
    {
        this->r += c.r;
        this->i += c.i;

        return (*this);
    }

    friend std::ostream& operator<<(std::ostream& out, const Compelx& c)
    {
        out << "r = " << c.r << ", i = " << c.i;

        return out;
    }

private:
    double r;
    double i;
};

int main()
{
    Compelx c1(5.6,4.5);
    Compelx c2(2.3,3.3);

    cout << c1 << endl;
    cout << c2 << endl;

    Compelx c3 = c1 + c2;
    cout << c3 << endl;

    Compelx c4 = c1 + 2;
    cout << c4 << endl;

    Compelx c5 = c1 - 2;
    cout << c5 << endl;

    return 0;
}


~~~~~~我的明天我知道~~~。
2016-11-12 19:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
不知道错在哪儿,下载个编译器,编译时编译器会告诉你错在哪儿
比如 Complex operator+(double a) 参数不足等等
2016-11-12 20:08
柚夏怪
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-9-7
收藏
得分:0 
回复 3楼 rjsp
这是编译器的提示,可是他提示的地方我感觉都没有错
=====运行开始======
【Line:3,Column:17】:需要';'
【Line:3,Column:26】:需要<标识符>
【Line:3,Column:27】:非法的类型开始
【Line:3,Column:28】:需要<标识符>
【Line:3,Column:34】:需要';'
【Line:3,Column:36】:非法的类型开始
【Line:3,Column:37】:需要<标识符>
【Line:3,Column:38】:需要';'
【Line:4,Column:26】:需要';'
【Line:4,Column:36】:需要';'
【Line:5,Column:17】:需要';'
【Line:5,Column:26】:需要<标识符>
【Line:5,Column:27】:非法的类型开始
【Line:5,Column:28】:需要<标识符>
【Line:5,Column:33】:需要';'
【Line:5,Column:42】:非法的类型开始
【Line:5,Column:44】:需要';'
【Line:8,Column:7】:非法的类型开始
【Line:8,Column:8】:需要';'
【Line:9,Column:10】:非法的类型开始
【Line:9,Column:11】:需要<标识符>
【Line:9,Column:17】:需要';'
【Line:9,Column:19】:非法的类型开始
【Line:9,Column:20】:需要<标识符>
【Line:9,Column:26】:需要';'
【Line:9,Column:28】:非法的类型开始
【Line:9,Column:29】:需要<标识符>
【Line:9,Column:31】:需要';'
【Line:9,Column:33】:非法的类型开始
【Line:9,Column:35】:需要';'
【Line:9,Column:37】:方法声明无效; 需要返回类型
【Line:9,Column:40】:需要<标识符>
【Line:10,Column:19】:需要';'
【Line:10,Column:34】:需要<标识符>
【Line:10,Column:37】:需要<标识符>
【Line:10,Column:39】:非法的类型开始
【Line:10,Column:44】:需要<标识符>
【Line:11,Column:4】:需要';'
【Line:12,Column:20】:方法声明无效; 需要返回类型
【Line:12,Column:29】:需要<标识符>
【Line:12,Column:30】:需要';'
【Line:12,Column:31】:非法的类型开始
【Line:12,Column:35】:需要';'
【Line:12,Column:39】:需要<标识符>
【Line:14,Column:3】:需要class, interface或enum
【Line:17,Column:3】:需要class, interface或enum
【Line:21,Column:3】:需要class, interface或enum
【Line:25,Column:3】:需要class, interface或enum
【Line:27,Column:1】:需要class, interface或enum
【Line:30,Column:1】:需要class, interface或enum
【Line:34,Column:1】:需要class, interface或enum
【Line:38,Column:1】:需要class, interface或enum
【Line:42,Column:3】:需要class, interface或enum
【Line:43,Column:3】:需要class, interface或enum
【Line:44,Column:3】:需要class, interface或enum
【Line:45,Column:3】:需要class, interface或enum
【Line:46,Column:3】:需要class, interface或enum
【Line:47,Column:3】:需要class, interface或enum
【Line:48,Column:3】:需要class, interface或enum
【Line:49,Column:3】:需要class, interface或enum
【Line:50,Column:3】:需要class, interface或enum
【Line:51,Column:1】:需要class, interface或enum
=====运行结束======
2016-11-13 06:38
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
class Complex
{
    friend Complex operator+( double f, const Complex& c );
    friend Complex operator-( double f, const Complex& c );

public:
    Complex( double real, double imag ) : real_(real),imag_(imag)
    {
    }
    Complex operator+( const Complex& c ) const
    {
        return Complex(real_+c.real_,imag_+c.imag_);  

    }
    Complex operator+( double f ) const
    {
        return Complex(real_+f,imag_);
    }
    Complex operator-( const Complex& c ) const
    {
        return Complex(real_-c.real_,imag_-c.imag_);
    }
    Complex operator-( double f ) const
    {
        return Complex(real_-f,imag_);
    }
    Complex operator-( void ) const
    {
        return Complex(-real_,-imag_);
    }

private:
    double real_;
    double imag_;
};

inline Complex operator+( double f, const Complex& c )
{
    return Complex(f+c.real_,c.imag_);
}
inline Complex operator-( double f, const Complex& c )
{
    return Complex(f-c.real_,-c.imag_);
}

int main( void )
{
    Complex c1(5.6,4.5);
    Complex c2(2.3,3.3);
    Complex c3 = c1 + c2;
    Complex c4 = c1 - c2;
    Complex c5 = c1 + 2.3;
    Complex c6 = c1 - 2.3;
    Complex c7 = 2.3 + c1;
    Complex c8 = 2.3 - c1;
    Complex c9 = -c1;

    return 0;
}
2016-11-14 08:26
快速回复:运算符重载的问题 不知道错在了哪里
数据加载中...
 
   



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

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