Eneko
Eneko

Reputation: 159

Don't know why I receive `error: forward declaration of <class>`

I have this code:

class AP_InertialSensor_Backend;

class AP_InertialSensor
{
public:
    const Vector3f  &get_gyro(uint8_t i) const { return drivers[primary_instance]->_gyro[i]; }
private:
    AP_InertialSensor_Backend *drivers[INS_MAX_INSTANCES];

    void detect_instance(uint8_t instance);    

    /// primary IMU instance
    uint8_t primary_instance=0;
};

#include <AP_InertialSensor_Backend.h>

Anyway, I am receiving these errors:

In member function 'const Vector3f& AP_InertialSensor::get_gyro(uint8_t) const':
error: invalid use of incomplete type 'class AP_InertialSensor_Backend'
error: forward declaration of 'class AP_InertialSensor_Backend'

I have other same projects and it worked then, but I do not know what is failing.

I would apreciate any help.

Thanks in advance.

Upvotes: 1

Views: 2276

Answers (2)

Logicrat
Logicrat

Reputation: 4468

You get this error because you have not included the header file for AP_InertialSensor_Backend before you use it. You did include a forward reference, but the only thing that allows you to do is declare a pointer to that class.

Upvotes: 0

Henrik
Henrik

Reputation: 23324

You're accessing a member _gyro of AP_InertialSensor_Backend before the class is defined.

To fix it, move the implementation of get_gyro to the cpp file.

Upvotes: 3

Related Questions