此代码在第 6 行抛出错误。是因为 cout 流不允许它还是在 ostream 中有一些冲突?
This code throws an error at line 6. Is it because cout stream doesn't allow it or it's some conflict in ostream?
#include<iostream>
using namespace std;
int main() {
int a=4,b;
cout<<b=a*a;
return 0;
}
显示
"error: no match for 'operator=' (operand types are 'std::basic_ostream<char>' and 'char')"
如果它必须对 cout 做些什么,谁能告诉我 cin 和 cout 是如何工作的?
运算符优先级见此处:https://en.cppreference.com/w/cpp/language/operator_precedence。
<<
排名 7。=
排名 16。*
排名 5。因此该行被解析为
(std::cout << b ) = (a * a);
您不能将 int
分配给 std::cout
。改为这样写:
int a = 4;
int b = a*a;
std::cout << b;
#include<iostream>
using namespace std;
int main() {
int a=4,b;
cout<<b=a*a;
return 0;
}
显示
"error: no match for 'operator=' (operand types are 'std::basic_ostream<char>' and 'char')"
如果它必须对 cout 做些什么,谁能告诉我 cin 和 cout 是如何工作的?
运算符优先级见此处:https://en.cppreference.com/w/cpp/language/operator_precedence。
<<
排名 7。=
排名 16。*
排名 5。因此该行被解析为
(std::cout << b ) = (a * a);
您不能将 int
分配给 std::cout
。改为这样写:
int a = 4;
int b = a*a;
std::cout << b;