为什么我的方程给出错误必须有一个积分表达式或无作用域enun类型
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.
你不能xor浮点数。如果你想对一个浮点数求立方,最简单的方法就是将它乘以三次。
你有两个错误第一个可能会给你一堆错误代码
cin << radius;
正确的结构是:
cin >> radius;
第二个错误是你实际要求的:
volume = 4 / 3 * PI * r ^ 3;
^ -是对浮点类型不起作用的异或操作符。从4/3*PI*r得到的结果类型是double。你不能对它做任何事。
我想你实际上是在试图得到结果的3次方。在c++中,没有简单的操作符可以做到这一点。您可以像这样使用pow(..)函数:
volume = pow(4 / 3 * PI * r, 3);
请记住添加#include <cmath>
才能使用该功能。
相关文章:
- 未在作用域中声明unordered_map
- 有没有一种方法可以在编译时获得作用域类名
- C++quit()函数中可能存在作用域问题
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 全局作用域中函数指针的赋值
- 在类函数中初始化外部作用域变量
- 不同作用域中的静态变量和全局变量
- 表达式必须具有完整或无作用域的枚举图
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- C++/错误:表达式必须具有整数或无作用域枚举类型
- C++ 在方程中使用变量;错误:表达式必须具有整数或无作用域枚举类型及其他
- 错误:以增量方式填充向量时,表达式必须具有整数或无作用域枚举类型
- 错误:数组大小表达式必须具有整型或无作用域枚举类型,而不是'double'
- 在c++中,全局作用域中只允许"表达式"初始化全局对象.我在哪里可以在标准中找到这个
- 表达式必须具有整数或无作用域枚举类型,并带有字符串向量
- GetBValue 错误:表达式必须具有整数或无作用域枚举类型
- 重载操作符+:表达式必须是整型或无作用域枚举类型
- 为什么我的方程给出错误必须有一个积分表达式或无作用域enun类型
- 表达式必须具有整型或无作用域枚举类型
- 表达式必须具有整型或无作用域枚举类型