ostream<<在课堂上超载粉碎

时间:2012-04-21 10:29:41

标签: c++ operator-overloading ostream

当我宣布ostream<<在我的复数类的重载方法中,它突然崩溃了 这是

#include<math.h>
#include<ostream>
#include<iostream>

class complex
{

public:
    double getRe();
    double gerIm();
    void setRe(double value);
    void setIm(double value);
    explicit complex(double=0.0,double=0.0);
    static complex fromPolar(double radius,double angle);
    complex operator+(complex rhs);
    complex operator-(complex rhhs);
    complex operator*(complex rhs);
    complex operator+(double rhs);
    complex operator-(double rhs);
    complex operator*(double rhs);
    complex conjugate();
    double norm();
    complex operator/(double rhs);
    complex operator/(complex rhs);
     friend ostream &operator<<(ostream &out, complex c);
private:
    double real;
    double img;

};
 ostream &operator<<(ostream &out, complex c)
{
    out<<c.real<<"  ";
    out<<c.img<<"  ";
    return out;


}
complex operator+(double lhs,complex rhs);
complex operator-(double lhs,complex rhs);
complex operator*(double lhs,complex rhs);
complex operator/(double lhs,complex rhs);
complex exp(complex c);
inline double complex::getRe(){return real;}
inline double complex::gerIm(){ return img;}
inline void complex::setRe(double value) {  real=value;}
inline void complex::setIm(double value) { img=value;}
 inline complex::complex(double re,double im) :real(re),img(im){}
 inline   complex complex::fromPolar(double radius,double angle){

     return complex(radius*cos(angle),radius*sin(angle));

 }
 inline complex complex::operator+(complex rhs)
 {
     return complex(this->real+rhs.real,this->img+rhs.img);

 }
 inline complex complex::operator-(complex rhs)
 {
     return complex(this->real-rhs.real,this->img-rhs.img);

 }
 inline complex complex::operator*(complex rhs)
 {
     return complex(this->real*rhs.real-this->img*rhs.img,this->real*rhs.img+this->img*rhs.real);

 }
 inline complex complex::operator+(double rhs)
 {
     return complex(this->real+rhs,this->img);

 }

 inline complex complex::operator-(double rhs)
 {
     return complex(this->real-rhs,this->img);

 }
 inline complex complex::operator*(double rhs)
 {
     return complex(this->real*rhs,this->img*rhs);

 }
 inline complex complex::operator/(double rhs)
 {
     return complex(this->real/rhs,this->img/rhs);

 }
 inline complex complex::operator/(complex rhs)
 {

     return (*this)*rhs.conjugate()/rhs.norm();


 }

 inline double complex::norm()
 {
 return (this->real*this->real+this->img*this->img);
 }

 inline complex complex::conjugate()
 {

     return complex(this->real,-this->img);
 }


 inline complex operator+(double lhs,complex rhs)
 {
     return rhs+lhs;
 }

 inline complex operator-(double lhs,complex rhs)
 {
     return complex(lhs-rhs.getRe(),rhs.gerIm());

 }
 inline complex operator*(double lhs,complex rhs)
 {
     rhs*lhs;

 }

 inline complex operator/(double lhs,complex rhs)
 {
     return rhs.conjugate()*lhs/rhs.norm();

 }

错误说,它是重新定义ostream运算符,但我认为我已经正确编写了,所以无法理解发生了什么,请帮帮我

2 个答案:

答案 0 :(得分:4)

ostream位于std命名空间中,因此您需要在类定义中使用:

friend std::ostream &operator<<(std::ostream &out, complex c);

,相应的定义应如下所示:

std::ostream &operator<<(std::ostream &out, complex c)
{
// ...

此外,您需要在operator*重载之一中使用return语句:

inline complex operator*(double lhs,complex rhs)
{
    return rhs*lhs;
}

当您在代码中使用与标准库类模板相同的名称时,不应使用using namespace std;。 (即使不是这种情况,在大多数情况下你应该避免使用using namespace std;,当然也应该在头文件中避免它。)

答案 1 :(得分:0)

如果你做了Charles Bailey所说的但仍然得到错误,请确保在文件顶部包含iostream。