没有运算符“>>”匹配这些操作数操作数类型为:std::istream >> double

问题描述 投票:0回答:3

对于我的项目,我试图为复数类创建一个自由函数。它是在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;
}

c++ complex-numbers non-member-functions
3个回答
2
投票
No operator ">>" matches these operands operand types are: std::istream >> double

这可能看起来像是一个奇怪的错误消息。毕竟,人们可以很快地想出一个示例程序,从

double
流式传输
std::cin
,这是一个
std::istream
。那么这里出了什么问题?

答案就在这条错误消息后面的一堆注释中。 (是的,这可能是一个令人生畏的混乱,但是注释可以帮助诊断问题。不,我不希望将整个混乱复制到问题中,因为它很大并且大部分都是不相关的。)候选运算符列表中的某个位置是

operator>>(double& __f)

这是允许流式传输

double
的运算符。但是,请注意参数的类型 - 它不是
double
,而是
double&
。流的目标必须命名变量,而不仅仅是提供值。就您而言,尝试
in >> z.real()
与尝试
in >> 3.1
类似。
z.real()
3.1
的类型都是
double
,所以你能对其中一个做的事,也能对另一个做。希望您不相信可以通过将新值输入
3.1
来改变数学。同样,您不能将值流式传输到返回
double
的函数中。

一种解决方案是让您的函数返回流运算符期望的内容,如

double& real()
所示(添加 & 符号并删除
const
)。然而,向私有成员提供公共非常量引用会破坏封装性;那时该成员也可能是公开的。另外,您的项目不允许这样做。让我们寻找更好的方法。

更常见的解决方案是使

operator>>
成为您班级的
friend
,以便它可以设置私有成员。这需要添加行

friend std::istream& operator>>(std::istream& in, complex& z);

您的类定义。完成后,您的

operator>>
实现可以绕过访问器函数来访问私有数据成员。注意:如果需要,
operator>>
的定义可以保留在原来的位置,在类定义之外。

std::istream& operator>>(std::istream& in, complex& z) {
    in >> z.realX;
    in >> z.imaginaryY;
    return in;
}

更迂回的方法使用构造和分配而不是友谊。这可能会减少更复杂情况下的代码重复。但是,在您的情况下,它会触发警告,因为您的类通过具有复制构造函数但没有赋值运算符而违反了“三规则”。尽管如此,编译器仍会自动为您生成显式复制构造函数。您可以通过注释掉复制构造函数来解决该警告。 std::istream& operator>>(std::istream& in, complex& z) { double real; double imaginary; in >> real; in >> imaginary; z = complex{real, imaginary}; return in; }

对于像你的
complex

课程这样简单的事情,我会带着友谊去。

    


1
投票

#include <sstream> ostringstream ss; ss << "SOCKET #" << sock << ":" << buf << "\r\n";



0
投票
#include <istream>

...我正在使用 CLion Nova Build #CL-233.13871。希望有帮助!

    

© www.soinside.com 2019 - 2024. All rights reserved.