对于我的项目,我正在尝试为复数类创建一个免费函数。它在 cpp 文件中定义。该函数是一个重载的输入流操作符,但我不断收到错误
No operator ">>" matches these operands operand types are: std::istream >> double
线上
in >> z.real();
in >> z.imag();
我创建了一个名为的文件complex.h
,其中包含complex
类和两个我想要工作的重载运算符、复数的构造函数(不确定是否需要但包括在内),以及两个用于检索复杂类的实部和虚部的 getter 方法。这会重现错误。
成员函数的声明由我的项目规范决定。它们的返回类型不能更改。
#pragma once
#include <iostream>
#include <cmath>
class complex {
private://may include private helper functions
double realX = 0;
double imaginaryY = 0;
public:// interface for operators and member functions (methods)
//**********************Constructors***************************
complex() {}
complex(double x) {
realX = x;
}
complex(double x, double y) {
realX = x;
imaginaryY = y;
}
complex(const complex& z) : realX(z.realX), imaginaryY(z.imaginaryY) { //copy constructor
}
double real() const {
return realX;
}
double imag() const {
return imaginaryY;
}
};
std::istream& operator>>(std::istream& in, complex& z) {
in >> z.real();
in >> z.imag();
return in;
}
std::ostream& operator<<(std::ostream& output, const complex& z) {
output << "(" << z.real()
<< ", " << z.imag()
<< "i)";
return output;
}