- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在我的复数类中声明 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/
题目地址:https://leetcode-cn.com/problems/candy-crush/ 题目描述 This question is about implementing a basi
当我在我的复数类中声明 ostream #include #include class complex { public: double getRe(); double gerIm(
我在我的项目中使用 recharts(参见示例),有时在我使用 recahrts 组件的所有页面上出现错误(参见错误),我的应用程序崩溃,然后我重新安装节点模块并开始工作。我使用的是 recharts
我正在开发一个健身/健康 flutter 应用项目。 我的应用程序在 Android 上工作正常,但当我调用位置服务时,应用程序在 ios 中崩溃并立即停止工作。 我在 map View 页面内有一个
由于“Default-568@2x.png”,我遇到了“PNG crush”错误。当我取消选中项目中的压缩 PNG 文件选项时(项目 -> 目标 -> build设置 -> 打包 -> 压缩 PNG
我用 Java Swing 做了一个简单的程序。 如果你开始,导弹从左到右穿过 Canvas ,当你点击 Canvas 时,气球就形成了。 我在两个标签之间创建了 crush 事件。但是,效果不佳。有
如何创建像 candy crush 中那样的动画,例如 link 中所示的启动动画.然后,它们还有弹出按钮动画和开始(粒子爆炸)动画。知道如何在 android 中实现这些东西。我尝试了如下所示的按钮
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在开发一个应用程序,我对实现 Candy Crush Saga Type 倒计时生命系统的方法很感兴趣。我的应用程序是在 phonegap 中开发的(因此 html、css jquery 和 jq
我主要在 C/Fotran/numpy 中进行一些数值/物理模拟,我使用 Java(尤其是 Processing 和 jMonkey)只是为了在代码原型(prototype)制作过程中获得一些实时交互
嗨, friend 们,我需要每 30 分钟增加一次生命的倒数计时器。所以我创建了一个倒数计时器,但它只在那个调用上运行。在这里我需要全局运行的定时器。如果在背景中应用或终止任何机构帮助我。 这是我的
我正在尝试为基于 JVM 的语言构建一个 cld3 包装器,使用 Java Abstracted Foreign Function Layer. 我创建了一个小类,将答案从 c++ 库转换为缓冲区。
不久前,我们从 Ceph 集群中删除了两个损坏的 OSD,osd.0 和 osd.8。它们现在从大多数 Ceph 命令中消失了,但仍然以奇怪的设备名称出现在 CRUSH 映射中: # devices
我在 iPhone 和 iPad 中使用 Cocos2d V3 创建了一个类似于 Candy Crush Saga 应用程序的应用程序。我想要糖果上的射线动画。光线应该从不同的方向和不同的距离通过。我
我是一名优秀的程序员,十分优秀!