[英]Invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
[英]Why am I getting this error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
我是初学者的定义。 我在学校的 Linux 服务器上使用 C++。 我已经在这个程序上工作了几个小时,但我不知道我做错了什么。 我重新分配了变量并重述了我的公式,但没有任何效果。 请帮忙。
#include<iostream>
#include<string>
using namespace std;
const int f=5;
int main ()
{
int a,b,c,d,e,sum,avg;
cout << "Please enter five numbers. " << endl;
cin >> a >> b >> c >> d >> e;
sum= a+b+c+d+e;
cout << "The average of those numbers is: " << endl;
cout << avg =(sum / f) << endl ;
return 0;
}
错误状态:'int' 和 '' 类型的无效操作数到二进制 'operator<<'
基本上问题是如何解析cout << avg =(sum / f) << endl
。
<<
是左关联的,并且具有比 = 更高的优先级,因此表达式被解析为
(cout << avg) = ((sum/f) << endl)
现在你的分配的右手边是int << endl
这会引发错误,因为操作没有意义( <<
它没有为int, decltype(endl)
参数定义)
这里是正确的代码......
#include<iostream>
#include<string>
using namespace std;
const int f=5;
int main ()
{
int a,b,c,d,e,sum,avg;
cout << "Please enter five numbers. " << endl;
cin >> a >> b >> c >> d >> e;
sum= a+b+c+d+e;
avg =(sum / f);
cout << "The average of those numbers is: " << endl;
cout << avg << endl ;
return 0;
}
输出:
Please enter five numbers.
1 2 3 4 5
The average of those numbers is:
3
问题出在此语句中- cout << avg =(sum / f) << endl ;
你可以写
cout<<sum/f<<endl;
或者你可以——
avg=sum/f;
cout<<avg<<endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.