错误:二进制'operator*' 'int [1]'和'float'类型的操作数无效

error: invalid operands of types 'int [1]' and 'float' to binary 'operator*'

本文关键字:float 类型 无效 操作数 int 二进制 operator 错误      更新时间:2023-10-16

运行代码时出现错误:

错误:类型为"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)。虽然您可以将intfloat相乘,但您不能将int[1]乘以float(这就是编译器抱怨的(。你要么要确保始终访问数组中数组的正确子元素,例如,state[4][0],要么首先不state数组数组......

除此之外,那一行还有一个错别字,请检查您的括号。