为什么我会收到此错误:类型"int"和"<未解析的重载函数类型>"到二进制"运算符<<的无效操作数

Why am I getting this error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’

本文关键字:lt 类型 重载 函数 二进制 操作数 无效 运算符 gt 错误 int      更新时间:2023-10-16

我是初学者的定义。我在学校的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"answers"类型的操作数对binary"运算符无效<lt;'

基本上问题是如何解析cout << avg =(sum / f) << endl

<<是左关联的,优先级高于=,因此表达式被解析为

(cout << avg) = ((sum/f) << endl)

现在,您的赋值的右侧是int << endl,这会引发错误,因为该操作没有意义(<<,它不是为int, decltype(endl)参数定义的(

问题出在这个语句-cout << avg =(sum / f) << endl ;中,您可以编写

cout<<sum/f<<endl; 

或者你可以-

avg=sum/f;
cout<<avg<<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