I get an error when I run my code:
error: invalid operands of types 'int [1]' and 'float' to binary 'operator*'
Codeblocks is telling me that the error appears in front of the "ct = ... "?
My code:
#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);
}
}
You declared state
to be an array of 5 arrays of 1 int
each. Since state
is an array of arrays, state[i]
is itself an array. So when you write something like, eg, state[4]*dt
what you're trying to do here is multiply an array ( state[4]
is an array of one int
) with a float
( dt
is a float)
. While you can multiply an int
with a float
, you cannot multiply an int[1]
with a float
(that's what the compiler is complaining about). You will either want to make sure to always access the right subelement of the array inside your array, eg, state[4][0]
, or not make state
an array of arrays in the first place…
Apart from that, there's also a typo in that line, check your brackets.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.