C++ 错误消息显示为";表达式必须具有整型或枚举类型;在c++;

C++ 错误消息显示为";表达式必须具有整型或枚举类型;在c++;,c++,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 o

我有下面的代码,我得到了这个等式的错误:

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(); 
}
#包括
#包括
使用名称空间std;
int main()
{
浮动v,p,r;
int n;
coutp;
库特;
coutn;
v=(p)*(1+r)^n;//这里我得到的错误消息是“表达式必须具有整型或枚举类型”

coutC++11 5.12-位异或运算符

独占或表达式: 和表达 异或表达式和表达式 1执行通常的算术转换;结果是按位的 操作数的异或函数。运算符仅适用于整数 或非作用域枚举操作数


如果要计算v=(p)*(1+r)n,则需要进行更改

v=(p)*(1+r)^n;

C++
中,
^
XOR
(异或)运算符,例如
a=2^3;//a将是1


<> >查阅更多信息。

< P>问题是 ^ < /C> >不是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)

按位异或运算符^

仅适用于整数或无范围枚举操作数


< P>(C++标准)

为什么?问题的原因是什么?为什么会解决这个问题?谢谢HAVURHUWTAOTA::“队长更新了。谢谢。
powf(p * (1 + r), n)

// Or possibly the following depending on how you want the
// precedence to shake out
p * powf(1 + r, n)