'operator + ='不匹配(操作数类型为'std :: basic_ostream <char>'和'int')

先生

给出以下代码;

#include<iostream>
using namespace std;

int main(){
    int number_1 = 3;
    int result_1 = 10;
    result_1 += number_1;
    cout << ++result_1;
    cout << result_1 += number_1;
}

这行cout << result_1 += number_1;给我一个错误。

'operator + ='不匹配(操作数类型为'std :: basic_ostream'和'int')

另一方面,该程序cout << ++result_1;正在运行,没有任何问题。

谁能解释这个错误是什么原因,是什么原因?

songyuanyao
  1. 谁能解释这个错误是什么原因,是什么原因?

根据Operator Operator Precedenceoperator<<具有比更高的优先级operator+=,因此您的代码等效为:

(cout << result_1) += number_1;

whilecout << result_1将返回std::cout(即std::ostream&),然后operator+=尝试被调用std::ostream,但它不存在。这就是错误消息试图告诉您的内容。

您可以将其更改为:

cout << (result_1 += number_1) ;

或从根本上避免这种令人困惑的代码。

result_1 += number_1;
cout << result_1;
  1. 另一方面cout << ++result_1;运行没有任何问题。

Beasuse的operator++优先级高于operator<<因此,它等同于cout << (++result_1);并且会很好。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误:“operator<<”不匹配(操作数类型为“std::basic_ostream<char>”和“<unresolved重载函数类型>”)

'operator <<'不匹配(操作数类型为'std :: ostream'{aka'std :: basic_ostream <char>'}和'const std :: type_index')

错误:不匹配'operator >>'(操作数类型为'std :: istream'{aka'std :: basic_istream <char>'}和'const int')|

错误: ? 的操作数:具有不同类型的“int”和“std::basic_ostream<char>”

错误:'operator>>' 不匹配(操作数类型为 'std::istream' {aka 'std::basic_istream<char>'} 和 'Oper')

错误:'operator[]' 不匹配(操作数类型是 'std::map<std::__cxx11::basic_string<char>, int>' 和 'char')

'operator ='不匹配(操作数类型为'std :: vector <int>'和'int'

'operator[]' 不匹配(操作数类型是 'std::unique_ptr<std::vector<int> >' 和 'int')

错误:与“ operator +”不匹配(操作数类型为“ std :: __ cxx11 :: list <int>”和“ int”)|

Android NDK16错误(“运算符'<<'不明确(操作数类型为'basic_ostream <char,std :: char_traits <char>>'和'long long'))”

如何理解C ++错误,“不匹配'operator =='(操作数类型为'std :: pair'和'const int')”?

出于某种原因,我不断收到此错误:“operator>>”不匹配(操作数类型为“std::istream”{aka 'std::basic_istream<char>'}

'operator+' 不匹配(操作数类型是 'std::vector' 和 'std::vector::size_type {aka long unsigned int}')

二进制表达式('std :: ostream'(aka'basic_ostream <char>')和'const char *')的无效操作数

二进制表达式的无效操作数('std::__1::ostream'(又名'basic_ostream<char>')和'vector<char>')

int在运算符==中显示为std :: basic_ostream <char>

错误:“ operator []”不匹配(操作数类型为“ QVector <int>”和“ QCharRef”)

错误:“ operator +”不匹配(操作数类型为“ int”和“ class”)

二进制表达式的无效操作数('std::ostream'(又名'basic_ostream<char>')和'sound_ctl')

二进制 '<<' : 未找到采用类型为 'std::basic_ostream<char, std::char_traits<char>>' 的左操作数的运算符

与'operator ='不匹配(操作数类型为'__gnu_cxx :: __ alloc_traits <std :: allocator <std :: vector <int>>>

重载运算符'<<'的用法不明确(操作数类型为'ostream'(又名'basic_ostream <char>')和'Person')

对二进制表达式无效的操作数(“ ostream”(又名“ basic_ostream <char>”)和“ ostream”)

二进制表达式的无效操作数(“ ostream”(又名“ basic_ostream <char>”)和“ void”)

无效的二进制二进制操作数('ostream'(aka'basic_ostream <char>')和'Date')

'int' 和 'const char [15]' 类型的无效操作数到二进制 'operator<<' ^

与“ operator >>”不匹配(操作数类型为“ QDataStream”和“ QJsonObject”)

如何修复:错误:“operator=”不匹配(操作数类型为“Estado”和“Estado*”)

不匹配“operator=”(操作数类型为“String”和“void”)