【问题标题】:ostream << overloading crush in classostream << 在课堂上重载粉碎
【发布时间】:2012-05-02 17:47:05
【问题描述】:

当我在我的复数类中声明 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运算符,但我认为我写得正确,所以无法理解发生了什么,请帮助我

【问题讨论】:

  • 您使用的是哪个编译器?我无法重现这种行为(g++ 4.6.2 MinGW-32bit)。
  • 这是一个头文件吗,因为ostream &amp;operator&lt;&lt;(ostream&amp;, complex) 是您尚未声明inline 的一个函数?
  • 另外,您所说的“突然崩溃”是什么意思,因为您引用的错误听起来像是编译或链接错误?
  • 编译时没有,给我很多错误
  • 当我编译你的代码时,我也得到一个错误,但我得到的错误是“'ostream'没有命名类型”,因为你没有使用所需的ostream 命名空间来限定ostream限定符,因此您似乎没有发布所有需要的详细信息来回答您看到错误的原因。

标签: c++ operator-overloading ostream


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您按照 Charles Bailey 所说的进行操作,但仍然收到错误消息,请确保在文件顶部包含 iostream。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 2019-05-13
      • 2015-03-31
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多