标题:求高手帮忙! c++ 问题?
只看楼主
DreamSkyCC
Rank: 2
来 自:河北赵县
等 级:论坛游民
帖 子:40
专家分:58
注 册:2010-11-28
结帖率:100%
已结贴  问题点数:2 回复次数:5 
求高手帮忙! c++ 问题?
求高手帮忙! c++ 问题?
#include <iostream>
using namespace std;
class Complex
{
    private:
  
        float Real,Image;
    public:
     
        Complex(float r = 0,float i = 0)
        {
            Real = r;
            Image = i;
     
        }
  
        void Show(int i)
        {
            cout<<"c"<< i << "=" << Real << "+" << Image << "i" << endl;
         
        }
        Complex operator+ (Complex &c);
     
        Complex operator- (Complex &c);
     
        void operator= (Complex &c);         

  
};
void Complex::operator= (Complex &c)
{
  
  

    Real = c.Real;
    Image = c.Image;
    return ;  

}
Complex Complex::operator+ (Complex &c)
{
  
    Complex t;
    t.Real = Real + c.Real;
    t.Image = Image + c.Image;
    return t;
}
Complex Complex::operator- (Complex &c)
{
  
    Complex t;
    t.Real = Real - c.Real;
    t.Image = Image - c.Image;
    return t;
}
int main(void)
{
    Complex c1(20,20),c2(30,50),c3;
    c1.Show(1);
    c2.Show(2);
    c3 = c1 + c2;
    c3.Show(3);
     

    return 0;
}
80: error: no match for ‘operator=’ in ‘c3 = (((Complex&)(& c2)))’
chongzai.cpp:36: note: candidates are: void Complex::operator=(Complex&)
搜索更多相关主题的帖子: class Complex private include 
2010-12-08 16:32
ljt
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:191
专家分:431
注 册:2009-4-6
得分:1 
怪哉,我电脑上能运行,没错
2010-12-08 17:24
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:705
专家分:2043
注 册:2010-11-11
得分:1 
估计又是一个编译器差异的问题
2010-12-08 20:19
Mack2
Rank: 1
来 自:山东
等 级:新手上路
帖 子:8
专家分:2
注 册:2010-11-26
得分:1 
#include <iostream>
using namespace std;
class Complex
{
    private:
  
        float Real,Image;
    public:
     
        Complex(float r = 0,float i = 0)
        {
            Real = r;
            Image = i;
     
        }
  
        void Show(int i)
        {
            cout<<"c"<< i << "=" << Real << "+" << Image << "i" << endl;
         
        }
        Complex operator+ (Complex &c);
     
        Complex operator- (Complex &c);
     
        Complex operator= (Complex &c);         

  
};
Complex Complex::operator= (Complex &c)
{

    Real = c.Real;
    Image = c.Image;
    return Complex(Real,Image) ;  

}
Complex Complex::operator+ (Complex &c)
{
  
    Complex t;
    t.Real = Real + c.Real;
    t.Image = Image + c.Image;
    return t;
}
Complex Complex::operator- (Complex &c)
{
  
    Complex t;
    t.Real = Real - c.Real;
    t.Image = Image - c.Image;
    return t;
}
int main(void)
{
    Complex c1(20,20),c2(30,50),c3,c4;
    c1.Show(1);
    c2.Show(2);
    c3 = c1 + c2;
    c3.Show(3);
    c4=c3;
    c4.Show(4);
    return 0;
}
重载赋值运算符时,返回类型不对。
2010-12-08 22:57
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:705
专家分:2043
注 册:2010-11-11
得分:0 
返回类型时对的,问题出在编译器上了
2010-12-08 23:27
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:605
专家分:2489
注 册:2009-4-8
得分:1 
以下是引用Mack2在2010-12-8 22:57:19的发言:

Complex Complex::operator= (Complex &c)
{

    Real = c.Real;
    Image = c.Image;
    return Complex(Real,Image) ;  

}
重载赋值运算符时,返回类型不对。
程序代码:
Complex& Complex::operator= (const Complex &c)
{
    Real = c.Real;
    Image = c.Image;
    return *this ; 
}
这样能更好些

迭代的是人,递归的是神。
2010-12-09 08:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-328375-1-1.html




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

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