C++操作员重载错误:‘运算符无匹配(操作数类型是‘ float’ and‘ class’)

发布于 2025-01-29 21:14:59 字数 949 浏览 1 评论 0原文

我正在研究一些OOP代码,并试图用操作员>>设置istream&。但是有了我的代码,我会不断遇到一个错误,就像标题中上面所述的那样。我必须执行此操作的代码是:

istream& operator>>(istream& in, Forward& fwd){
    in >> fwd.value;
    cerr << "Value in forward is: " << fwd.value;

    return in;
}

此代码是forward类的一部分,value是base类的成员变量,称为命令 ,哪个forward正在继承。

然后,我尝试在其他类中使用此操作员,program类:

istream& operator>>(istream& in, Program& prog){
    float value = 4;
    Forward *temp = new Forward();
    value >> *temp;
}

但这给了我错误:

错误:“操作员&gt;&gt;''无匹配(操作数类型是“ float”和“向前”)

没有已知的参数转换为“ float”到'std :: istream&amp; {aka std :: basic_istream&amp;}'

注:我需要保持行fortht *temp = new forned();,因为它在代码中经常使用。

I'm working on some OOP code and was trying to setup a istream& with the operator >>. But with the code that I have, I keep getting an error, like the one stated above in the title. The code I have to do this is:

istream& operator>>(istream& in, Forward& fwd){
    in >> fwd.value;
    cerr << "Value in forward is: " << fwd.value;

    return in;
}

This code is part of the Forward class and value is a member variable of the base class called Command, which Forward is inheriting from.

I then tried to use this operator in a different class, the Program class:

istream& operator>>(istream& in, Program& prog){
    float value = 4;
    Forward *temp = new Forward();
    value >> *temp;
}

But this gave me the errors:

error: no match for ‘operator>>’ (operand types are ‘float’ and ‘Forward’)

and

no known conversion for argument 1 from ‘float’ to ‘std::istream& {aka std::basic_istream&}’

Note: I need to keep the line Forward *temp = new Forward(); because it is used a lot in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2025-02-05 21:14:59
value >> *temp

替换为

in >> *temp;
value >> *temp

Replace with

in >> *temp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文