为什么我会收到此错误:类型"int"和"<未解析的重载函数类型>"到二进制"运算符<<的无效操作数
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"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
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- std::pair的默认构造函数<>将基本类型(int等)设置为零