标题:为什么我编的复数类不能执行除法运算了 ,能编译 ,不能生成
只看楼主
q236763612
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-10-22
 问题点数:0 回复次数:1 
为什么我编的复数类不能执行除法运算了 ,能编译 ,不能生成
不能执行除法运算了  有生成错误   哪位大哥能帮帮我哈哈!

#include<iostream>

#include<cassert>
using namespace std;
class Complex
{
private:
    int Real ;
    int Image;
public:
Complex(int r=0, int i=0)
:Real(r),Image(i)
{}
~Complex()



  Real=a.Real ;
  Image=a.Image ;
}
Complex& operator=(const Complex& a)
{
    assert(this!=&a);
   Real=a.Real;
  Image=a.Image ;
  return *this;
}
void print()
{
    cout<<Real<<"+"<<Image<<"i"<<endl;
}
friend Complex Add(const Complex& a, const Complex& b);
friend Complex Div(const Complex& a, const Complex& b);
};
Complex Add( const Complex& a, const Complex& b)
{
    return Complex (a.Real +b.Real ,a.Image+b.Image );

}
Complex Div ( const Complex& a , const Complex& b)
{
    assert( b.Real *b.Image !=0);
  return  Complex ( (a.Real *b.Real +a.Image*b.Image)/(b.Real*b.Real  +b.Image*b.Image), (a.Image *b.Real -a.Real *b.Image)/(b.Real*b.Real  +b.Image*b.Image ));
}
int  main ()
{
    Complex a,b,c;
  a.SetComplex ();
  b.SetComplex ();
  c=Div (a,b);
  c.print();
}

不能执行除法运算了  有生成错误   哪位大哥能帮帮我哈哈!
代码如下
#include<iostream>
#include<cassert>
using namespace std;
class Complex
{
private:
    int Real ;
    int Image;
public:
Complex(int r=0, int i=0)
:Real(r),Image(i)
{}
~Complex()



  Real=a.Real ;
  Image=a.Image ;
}
Complex& operator=(const Complex& a)
{
    assert(this!=&a);
   Real=a.Real;
  Image=a.Image ;
  return *this;
}
void print()
{
    cout<<Real<<"+"<<Image<<"i"<<endl;
}
friend Complex Add(const Complex& a, const Complex& b);
friend Complex Div(const Complex& a, const Complex& b);
};
Complex Add( const Complex& a, const Complex& b)
{
    return Complex (a.Real +b.Real ,a.Image+b.Image );

}
Complex Div ( const Complex& a , const Complex& b)
{
    assert( b.Real *b.Image !=0);
  return  Complex ( (a.Real *b.Real +a.Image*b.Image)/(b.Real*b.Real  +b.Image*b.Image), (a.Image *b.Real -a.Real *b.Image)/(b.Real*b.Real  +b.Image*b.Image ));
}
int  main ()
{
    Complex a,b,c;
  a.SetComplex ();
  b.SetComplex ();
  c=Div (a,b);
  c.print();
}
搜索更多相关主题的帖子: 除法 运算 编译 
2008-11-11 11:12
newyj
Rank: 2
等 级:新手上路
威 望:3
帖 子:542
专家分:0
注 册:2008-1-4
得分:0 
程序代码:
#include<iostream>
#include<cassert>
using namespace std;

class Complex
{
private:
    int Real ;
    int Image;
public:
Complex(int r=0, int i=0)
:Real(r),Image(i)
{}
~Complex(){}
Complex& operator=(const Complex& a)
{
    assert(this!=&a);
   Real=a.Real;
  Image=a.Image ;
  return *this;
}
void print()
{
    cout<<Real<<"+"<<Image<<"i"<<endl;
}
Complex Add(const Complex& a,const Complex& b);           //去掉friend
Complex Div(const Complex& a, const Complex& b);          //同上
};
Complex Complex::Add(const Complex& a,const Complex& b)
{
    return Complex(a.Real +b.Real ,a.Image+b.Image);
}
Complex Complex::Div( const Complex& a , const Complex& b)
{
    assert( b.Real *b.Image !=0);
  return  Complex ( (a.Real *b.Real +a.Image*b.Image)/(b.Real*b.Real  +b.Image*b.Image), 
                   (a.Image *b.Real -a.Real *b.Image)/(b.Real*b.Real  +b.Image*b.Image ));
}
int  main ()
{
    Complex a(1,2),b(2,3),c(3,4);
  //a.SetComplex();   //类中没有 定义 SetComplex()函数
  //b.SetComplex();
  c.Div(a,b);         //
  c.print();
  getchar();
  return 0;
}
2008-11-11 21:31



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




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

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