简体   繁体   English

为什么我会收到此错误:&#39;int&#39; 和 &#39; 类型的无效操作数<unresolved overloaded function type> &#39;转二进制&#39;运算符&lt;&lt;&#39;

[英]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.

相关问题 类型为&#39;int&#39;和&#39;的无效操作数 <unresolved overloaded function type> &#39;转换为二进制&#39;operator &lt;&lt;&#39; - Invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’ 错误:类型&#39;int&#39;和&#39;的操作数无效 <unresolved overloaded function type> &#39;到二元&#39;运算符/&#39; - error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator/’ 错误:'int' 和 ' 类型的无效操作数<unresolved overloaded function type> ' 到二进制 '运算符&lt;='</unresolved> - Error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<=' 错误:类型&#39;int&#39;和&#39;的操作数无效 <unresolved overloaded function type> &#39;到二元&#39;运算符&lt;&lt;&#39; - error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<' 错误:类型的无效操作数&#39; <unresolved overloaded function type> &#39;和&#39;int&#39;到二进制&#39;运算符&gt;&#39;if(min&gt; max) - error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator>' if(min > max) 为什么我收到这个错误? 错误:类型为“int”和“int(int, int)”的无效操作数转换为二进制“operator/” - Why am i Getting this error? error: invalid operands of types 'int' and 'int(int, int)' to binary 'operator/' 类型long long int的无效操作数和未解析的重载函数类型为二进制运算符+ - Invalid operands of types long long int and unresolved overloaded function type to binary operator+ 简单的程序与结构。 得到这个:类型&#39;void&#39;和&#39;的无效操作数 <unresolved overloaded function type> &#39;到二元&#39;运算符 - Simple program with a structure. Getting this: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator C++ 错误:&#39;double&#39; 和类型的无效操作数 - C++ error: invalid Operands of types 'double' and <unresolved overloaded function type' to binary 'operator' C ++错误:无效类型&#39; <unresolved overloaded function type> [int]&#39; - C++ error: invalid types '<unresolved overloaded function type>[int]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM