错误:二进制'operator^' 'float'和'int'类型的操作数无效
Error: invalid operands of types 'float' and 'int' to binary 'operator^'
我将类型为"float"answers"int"的无效操作数错误转换为二进制"operator ^",我不知道如何修复
错误发生在最后一行中的函数f中
非常感谢任何帮助
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
float f(float x);
int main()
{
float a;
float b;
int n;
float h;
float x;
float area;
cout << "Please input the first limit: ";
cin >> a;
cout << "Please input the second limit: ";
cin >> b;
cout << "How many rectangles do you want to use? ";
cin >> n;
h = (b-a)/n;
area = (f(a)+f(b))/2;
for (int i=1;i<n;i++) {
area += f(a+i*h);
}
area = area*h;
cout << "The area under the curve of f(x) = (2/sqrt(3.14))(exp(-x^2)) is ";
cout << area;
}
float f(float x){
return (exp(-x^2))(2/sqrt(3.14));
}
x
的数据类型为float
。您已经对其应用了逻辑XOR运算符。XOR需要整数操作数。
不过我怀疑你在找指数。C++没有指数运算符。相反,试试这样的东西:
float f(float x){
return (exp(-(x*x)))*(2/sqrt(3.14));
}
我假设你的意思是用exp(-(x*x))
乘以(2/sqrt(3.14)
,但我没有看到乘法运算符。
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 指针问题:从不兼容的类型"int"分配给"int *"
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 错误:数组下标的类型"int[int]"无效
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 从类型bankAccount的返回值到函数返回类型int没有可行的转换
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- 运行时错误:有符号整数溢出:964632435 * 10 无法在类型 'int' 中表示
- 如何增加模板类型 int 参数?
- 尝试使用 Array 类的模板构造整数数组时,错误:为什么数组类型"int [5]"不可分配,
- 如何为底层类型 int 的枚举专门化类
- 数组下标编译的无效类型'int[int]'由于 -Wfatal 错误而终止
- 不能将 "const char*" 类型的值分配给类型 "int" 的实体
- 数组下标的无效类型INT [INT]
- 请求在"伺服控制器"中请求成员"附加",该成员属于非类类型"int"
- 数组类型 'int [8]'不可分配
- 未声明的标识符和类型"int"从 main 调用类时出现意外错误