错误消息在 C++ 中显示为"expression must have integral or enum type"
Error message displaying as "expression must have integral or enum type" in c++
我有以下代码,我在这个等式中得到错误:
v=p*(1+r)^n.
请帮助我找到此错误的原因。
# include <iostream>
# include <limits>
using namespace std;
int main()
{
float v,p,r;
int n;
cout<<"Enter value of p:";
cin>>p;
cout<<"Enter value of r:";
cin>>r;
cout<<"Enter value of n:";
cin>>n;
v=(p)*(1+r)^n; // here i am getting error message as "expression must have integral or enum type"
cout<<"V="<<v;
std::cin.ignore();
std::cin.get();
}
C++11 5.12 - 按位独占 OR 运算符
独占或表达式: 和表达 独占或表达式 ˆ 和表达式 1 执行通常的算术转换;结果是按位 操作数的独占 OR 函数。运算符仅适用于积分 或无作用域枚举操作数。
如果要计算 v=(p)*(1+r)n,则需要更改
v=(p)*(1+r)^n;
自
v = p * powf(1+r, n); // powf: exponential math operator in C++
在C++
中,^
是XOR
(独占或)运算符,例如 a = 2 ^ 3; // a will be 1
.
查看此处了解更多信息。
问题是^
不是C++的指数数学运算符,而是按位异或运算。 按位运算只能对整数/枚举值进行。
如果要将浮点数提高到特定幂,请使用 powf
函数
powf(p * (1 + r), n)
// Or possibly the following depending on how you want the
// precedence to shake out
p * powf(1 + r, n)
按
位独占 OR 运算符 ^
仅适用于整型或无作用域枚举操作数
(C++标准)
相关文章:
- 为什么static_assert错误:即使我传递常量"expression must have a constant value"?
- 错误:"Left of getValue must have class/struct/union"
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- 编译为 cuda 内核调用提供了"expression must have integral or unscoped enum type"
- 调用 list.size() 方法给出错误"expression must have class type"
- 为什么我会收到"Expression must have a class type"错误?(C++)
- C++ 中数组的"expression must have pointer-to-object type"是什么?
- 当值不能恒定时如何修复"Expression must have a constant value"?
- 仅当链接函数 C++ 时,才会发生"Expression must have class type error"
- 我可以忽略"Intellisense: (E0028) expression must have a constant value"吗?
- 得到错误"left of '.toString' must have class/struct/union",即使 toString 应该有一个合适的对象
- 字符串初始化失败:"expression must have integral or unscoped enum type"
- 错误 - "expression must have a class type" - c++
- "Expression must have class type"错误
- deque.push_front() 给出错误"expression must have class type"
- 使用 "this" 关键字时的"Error: expression must have a pointer type"
- 使用可变宏时获得"Error: #28: expression must have a constant value "
- 在 2D 阵列分配期间对 calloc 进行"expression must have (pointer-to) function type"
- 当整数变量用于在 c++ 中声明数组大小时,错误显示为"Expression must have a const value"
- 错误消息在 C++ 中显示为"expression must have integral or enum type"