[英]Why am I getting this error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
I am the definition of a beginner.我是初学者的定义。 I am working with C++ in my school's Linux server.
我在学校的 Linux 服务器上使用 C++。 I have been working at this program for a few hours and I can't figure out what I'm doing wrong.
我已经在这个程序上工作了几个小时,但我不知道我做错了什么。 I've reassigned variables and restated my formulas but nothing works.
我重新分配了变量并重述了我的公式,但没有任何效果。 Please help.
请帮忙。
#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;
}
The error states: invalid operands of types 'int' and '' to binary 'operator<<'错误状态:'int' 和 '' 类型的无效操作数到二进制 'operator<<'
Basically the problem is how cout << avg =(sum / f) << endl
is parsed.基本上问题是如何解析
cout << avg =(sum / f) << endl
。
<<
is left associative and has higher precedence than = so the expression is parsed as <<
是左关联的,并且具有比 = 更高的优先级,因此表达式被解析为
(cout << avg) = ((sum/f) << endl)
Now the right hand side of your assignment is int << endl
which raises the error, since the operation makes no sense ( <<
it's not defined for int, decltype(endl)
arguments)现在你的分配的右手边是
int << endl
这会引发错误,因为操作没有意义( <<
它没有为int, decltype(endl)
参数定义)
Here the correct code....这里是正确的代码......
#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;
}
output:输出:
Please enter five numbers.
1 2 3 4 5
The average of those numbers is:
3
The problem is in this statement- cout << avg =(sum / f) << endl ;
问题出在此语句中-
cout << avg =(sum / f) << endl ;
either you could write你可以写
cout<<sum/f<<endl;
or you could just-或者你可以——
avg=sum/f;
cout<<avg<<endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.