错误:二进制'operator*' 'int [1]'和'float'类型的操作数无效
error: invalid operands of types 'int [1]' and 'float' to binary 'operator*'
运行代码时出现错误:
错误:类型为"int [1]"和"float"的操作数无效,变为二进制"运算符*">
代码块告诉我错误出现在"ct = ..."?
我的代码:
#include <iostream>
#include <math.h>
using namespace std;
float dt = 0.01;
int p = 50;
float ct = 0;
int main()
{
int state [5][1] = {5,5,0,100,0 };
// cout << *state[3];
for (float i = 0; i < 10; i+dt){
ct = (state[0]+(2*state[4]/state[5])* sin(state[4]*dt/2)*cos(state[2+state[4])*dt/2);
}
}
您声明state
是一个由 5 个数组组成的数组,每个数组 1 个int
。由于state
是数组数组,因此state[i]
本身就是数组。因此,当你写一些类似的东西时,例如,state[4]*dt
你在这里试图做的是将数组(state[4]
是一个int
的数组(乘以一个float
(dt
是一个float)
。虽然您可以将int
与float
相乘,但您不能将int[1]
乘以float
(这就是编译器抱怨的(。你要么要确保始终访问数组中数组的正确子元素,例如,state[4][0]
,要么首先不state
数组数组......
除此之外,那一行还有一个错别字,请检查您的括号。
相关文章:
- 数组下标的类型"float*[float]"无效
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 编译器给出错误:format 指定类型 'float *',但参数的类型'double' [-Wformat]
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- "错误:数组下标的无效类型'float [10001][float]'是什么意思?
- 将"float*"赋值为"float* [1000]"中的不兼容类型
- 数组类型 'float [size]'不可分配
- 错误:二进制'operator*' 'int [1]'和'float'类型的操作数无效
- 功能使用INT和Float类型作为C 中的参数过载
- Qt C++ - 不允许使用 QList<float**> 类型
- 为什么主函数不识别其他功能是否返回float类型编号
- QList<float* 类型的 NULL 返回值>
- 为什么我的方法不从文件.txt中读取float类型
- 自定义构件上的 float 类型的Q_PROPERTY不会显示在属性编辑器中
- 为什么用“std::chrono”进行时差测量时,与“double”类型相比,用“float”类型给出的有效数字更多
- 在当前的C++和Java中,double类型和float类型:if(x==0.0)是正确的
- 将double/float类型转换为string类型,保留科学记数法和精度
- c++中两个float类型变量的精确比较
- 为什么要将float类型的指针转换为long类型的指针,然后解引用呢?
- 如何在OpenCV中限制float类型Mat精度