ostream << 课堂上超载的粉碎
ostream << overloading crush in class
当我在我的复数类中声明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;
,当然也应该避免在头文件中使用它。
如果您按照查尔斯·贝利所说的做了,但仍然收到错误,请确保在文件顶部包含iostream。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- 如何在课堂上重用和重新初始化 c++ discrete_distribution?
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- C++来自课堂的内存数据?
- 部分课堂上的互斥体
- 课堂上的一行,使整个应用程序坏了. 0xC000005错误
- 如何在课堂上正确使用升压通道(和光纤)?
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- 如何在课堂上进行多个打字重载
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 如何在课堂上访问私有结构
- 使用Mutex,lock_guard,在课堂中正确地lock
- 当记忆分配给课堂时
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;