C++操作员重载错误:‘运算符无匹配(操作数类型是‘ float’ and‘ class’)
我正在研究一些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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换为
Replace with