为什么我的等式给出错误必须有一个整数表达式或无范围的枚举类型?

Why is my equation giving Error must have an integral expression or unscoped enun type?

所以大家好,我想制作一个简单的程序来学习函数,但我 运行 遇到了这个以前从未遇到过的错误,有人可以帮助我吗?

#include<iostream>
#include<iomanip>
#include<string>


using namespace std;

float SphereVolume(float r);

int main() {

    /*

    Calculating a sphere.

    */
    float radius;
    cout << "Sphere Calc....\n";
    cout << "\tPlease Enter Radius of Sphere: ";
    cin << radius;

    SphereVolume(radius);

    return 0;
}


float SphereVolume(float r) {

    double const PI = 3.14159;
    float volume;

    volume = 4/3 * PI * r ^ 3; // error starts here.

    return volume;
}

我似乎无法理解为什么会发生这种情况,错误,当我尝试声明体积方程时开始,它说错误??

volume = 4/3 * PI * r ^ 3; // error starts here.

4 / 3 is integer division -> 1  

r ^ 3  is r xor-ed with the integer 3. 

你不能异或一个浮点数。如果要对浮点数进行立方,最简单的解决方案是将其乘以三倍。

你有两个错误,第一个错误可能给你一大堆错误代码:

cin << radius;

正确的构造是:

cin >> radius;

第二个错误是您实际要问的错误:

volume = 4 / 3 * PI * r ^ 3;

^ - 是一个异或运算符,不适用于浮点类型。 4/3*PI*r 的结果类型是 double。你不能对它做 xor (^)。
我认为您实际上是在尝试求出结果的 3 次方。在 C++ 中,没有简单的运算符可以做到这一点。你可以像这样使用 pow(..) 函数:

volume = pow(4 / 3 * PI * r, 3);

记得添加#include <cmath>才能使用该功能。