hblee595520133 2016-08-02 12:05 采纳率: 50%
浏览 6079
已结题

【初学者】请问cannot bind 'std::basic_ostream是什么原因?

primer plus 11章里的一道题:
头文件:
#include
using std::ostream;
using std::istream;

class Cmp
{
private:
double n_real;
double n_imaginary;
public:
Cmp(double real = 0, double imaginary = 0);
~Cmp();
friend Cmp operator+ (const Cmp &a, const Cmp &b);
friend Cmp operator- (const Cmp &a, const Cmp &b);
friend Cmp operator* (const Cmp &a, const Cmp &b);
friend Cmp operator* (double n, const Cmp &cmplx);
friend Cmp operator~ (const Cmp &cmplx);
friend std::istream &operator>> (std::istream &is, Cmp &cmplx);
friend std::ostream &operator<< (std::ostream &os, Cmp &cmplx);
};

函数定义:
#include
#include "cmp.h"

using std::cout;

Cmp::Cmp(double real, double imaginary)
{
n_real = real;
n_imaginary = imaginary;
}

Cmp::~Cmp() {}

Cmp operator+ (const Cmp &a, const Cmp &b)
{
return Cmp(a.n_real + b.n_real, a.n_imaginary + b.n_imaginary);
}

Cmp operator- (const Cmp &a, const Cmp &b)
{
return Cmp(a.n_real - b.n_real, a.n_imaginary - b.n_imaginary);
}

Cmp operator* (const Cmp &a, const Cmp &b)
{
return Cmp(a.n_real * b.n_real - a.n_imaginary * b.n_imaginary,
a.n_real * b.n_imaginary + a.n_imaginary * b.n_real);
}

Cmp operator* (const double n, const Cmp &cmplx)
{
return Cmp(n * cmplx.n_real, n * cmplx.n_imaginary);
}

Cmp operator~ (const Cmp &cmplx)
{
return Cmp(cmplx.n_real, -cmplx.n_imaginary);
}

std::istream &operator>> (std::istream &is, Cmp &cmplx)
{
cout << "real: ";
is >> cmplx.n_real;
cout << "imaginary: ";
is >> cmplx.n_imaginary;

return is;

}

std::ostream &operator<< (std::ostream &os, Cmp &cmplx)
{
os << "(" << cmplx.n_real << ", " << cmplx.n_imaginary << "i)";

return os;

}

应用:
#include
#include "cmp.h"

using namespace std;

int main()
{
Cmp a(3.0, 4.0); // initialize to (3,4i) Programming Exercises
Cmp c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a << "\n";
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";

return 0;

}

运行后程序报错 cannot bind 'std::basic_ostream' lvalue to 'std::basic_ostream&&'|
折腾了好长时间都解决不了,代码也感觉没有错误,请问大家到底问题出在哪里?

  • 写回答

2条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致