Error initializing stuct in source code

98 views Asked by At

I am using some code made by someone else, to implement a kalman filter into my imu with arduino. I understand the vast majority of it and the maths behind it, but i am having some errors when implementing it which i cannot understand (serious lack of knowledge on typdefs and structs and initializing them). If someone could explain this error to me it would be much appreciated.

The header file contains this...

struct kalman_data{

    float x1, x2, x3;

    float p11, p12, p13, 
          p21, p22, p23, 
          p31, p32, p33;

    float q1, q2, q3;
};

typedef struct _kalman_data kalman_data;

void kalman_data(kalman_data* data);
void kalman_calc(kalman_data* data, float z1, float z2);

My arduino code contains this..

kalman_data pitch_data;
kalman_data roll_data;

void loop(){

  kalman_data(&pitch_data);
  kalman_data(&roll_data);
  kalman_calc(&pitch_data, pitch, gyro_yz); 
  kalman_calc(&roll_data, roll, gyro_xz);
}

And this is the error i get...

In file included from test.ino:2:0: E:\Users\Alexander\Documents\Arduino\libraries\Kalman/kalman.h:30:29: error: conflicting declaration 'typedef struct _kalman_data kalman_data'
typedef struct _kalman_data kalman_data;

E:\Users\Alexander\Documents\Arduino\libraries\Kalman/kalman.h:17:8: error: 'struct kalman_data' has a previous declaration as 'struct kalman_data' struct kalman_data{

2

There are 2 answers

1
Sami Kuhmonen On BEST ANSWER

The struct definition is reserving the name kalman_data and the typedef is trying to reserve the name again, for a struct called _kalman_data. This causes the error.

To rectify this, use struct _kalman_data and then the typedef will work as intended.

0
Cheers and hth. - Alf On
struct kalman_data{

should be

struct _kalman_data{

in order to correspond to the subsequent

typedef struct _kalman_data kalman_data;

Then, the function

void kalman_data(kalman_data* data);

should better be renamed. As I recall both C and C++ allow you to reuse a type name for a function, to have both in scope at the same time, but even if that's allowed it's absolutely not a good idea. Very confusing.