, когда я объявляю 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, но я думаю, что я правильно написал, так что не могу понять, что происходит, пожалуйста, помогите мне
Какой у вас компилятор? Я не могу воспроизвести это поведение (g ++ 4.6.2 MinGW-32bit). – Zeta
Является ли это заголовочным файлом, потому что 'ostream & operator << (ostream &, complex)' - это единственная функция, которую вы не объявили 'inline'? –
Кроме того, что вы подразумеваете под «внезапным раздавливанием», потому что ошибка, которую вы цитируете, звучит как компиляция или ошибка ссылки? –