【发布时间】: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 &operator<<(ostream&, complex)是您尚未声明inline的一个函数? -
另外,您所说的“突然崩溃”是什么意思,因为您引用的错误听起来像是编译或链接错误?
-
编译时没有,给我很多错误
-
当我编译你的代码时,我也得到一个错误,但我得到的错误是“'ostream'没有命名类型”,因为你没有使用所需的
ostream命名空间来限定ostream限定符,因此您似乎没有发布所有需要的详细信息来回答您看到错误的原因。
标签: c++ operator-overloading ostream