c++ - ostream << 在类中重载 crush

标签 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操作符的重新定义,但我认为我写的正确,所以不明白这是怎么回事,请帮助我

最佳答案

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; 并且当然要避免在头文件中使用它。)

关于c++ - ostream << 在类中重载 crush,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258121/

相关文章:

c++ - 关于 C++ 中的流

c++ - 将 QT5 与 glLoadGen 结合使用

c++ - 从非常量对象调用 const 函数

c++ - 在 Visual Studio 中发布版本仍然更慢?

c++ - C++ IO 流简介

c++ - 重载 operator<<(ostream&, T) 其中 T 是 "enum class MyEnum"

c++ - 在窗口模式下设置/获取我的绝对鼠标位置

C++ 为赋值重载 operator()

C++ 运算符重载和继承

c++ - 如何使用具有多态性的插入运算符