2

I'm quite new to C++ and it might be a problem with me not knowing how to do things properly but I want my class _MPU6050 to have two functions that return a struct. I tried several forms of declaring the struct to no avail, I always get this error:

MPU6050.cpp:43: error: no 'int _MPU6050::SensorDataRaw::getRawGyroValues()' member function declared in class '_MPU6050::SensorDataRaw'
ISO C++ forbids declaration of 'getRawAccValues' with no type [-fpermissive]

As I understand, the compiler doesn't recognize the function _MPU6050::SensorDataRaw::getRawGyroValues() and tries to add the return type int at the beginning.

Here is my code:

MPU6050.cpp:

#include "MPU6050.h"

...

_MPU6050::SensorDataRaw MPU6050::getRawAccValues(){
  Wire2.beginTransmission(ADD);
  Wire2.write(GET_ALL_ACC);
  Wire2.endTransmission();
  Wire2.requestFrom(ADD, 6);
  sensorData.ax = Wire2.read()<<8 | Wire2.read();
  sensorData.ay = Wire2.read()<<8 | Wire2.read();
  sensorData.az = Wire2.read()<<8 | Wire2.read();
  return sensorData;
}

_MPU6050::SensorDataRaw MPU6050::getRawGyroValues(){
  Wire2.beginTransmission(ADD);
  Wire2.write(0x43);
  Wire2.endTransmission();
  Wire2.requestFrom(ADD, 6);
  sensorData.gx = Wire2.read()<<8 | Wire2.read();
  sensorData.gy = Wire2.read()<<8 | Wire2.read();
  sensorData.gz = Wire2.read()<<8 | Wire2.read();
  return sensorData;
}

MPU6050.h:

class _MPU6050
{
  public:
    struct SensorDataRaw{
      int16_t ax, ay, az, gx, gy, gz;
    };
  public:
    _MPU6050(void);
    void setXGyroOffset(int16_t offset);
    void setYGyroOffset(int16_t offset);
    void setZGyroOffset(int16_t offset);
    void setXAccOffset(int16_t offset);
    void setYAccOffset(int16_t offset);
    void setZAccOffset(int16_t offset);
    SensorDataRaw getRawAccValues();
    SensorDataRaw getRawGyroValues();
  private:
    SensorDataRaw sensorData;
};

Please let me know what I'm doing wrong. Thank you!

binux14
  • 23
  • 2
  • 5
    `_MPU6050` is a reserved identifier. You should use another name for the class. – eerorika Jan 27 '19 at 16:54
  • There is no return type in definition, this makes the compiler to look for a declaration which returns `int` It should be `SensorDataRaw _MPU6050::SensorDataRaw MPU6050::getRawGyroValues(){`. – Jack Jan 27 '19 at 16:54
  • 2
    What is `MPU6050` (without the leading underscore)? Looks like it might be defined as an empty macro. – 1201ProgramAlarm Jan 27 '19 at 16:58
  • 2
    You need to decide whether it's _MPU6050 or MPU6050, and stick with your decision. The second one is the right one, just FYI. Choose it if you control the code. – n. m. could be an AI Jan 27 '19 at 17:02
  • 1
    Obligatory link to [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Jan 27 '19 at 17:40
  • I will be changing the class name to MPU6050, wasn't aware of the rules for reserved identifier and I used the underscore because I was previously trying to use another class called the same. – binux14 Jan 28 '19 at 16:35

1 Answers1

1

_ is, in your case, part of the name of the class. It is not allowed to start any variable/type/function names with _ followed by an upper letter, so I would strongly recommend to remove this token.

In your specific case, you have not used _ when referring to your class name, so the compiler cannot find the class.

_MPU6050::SensorDataRaw MPU6050::getRawAccValues(){

The second MPU6050 does not have _ and is therefore a different name.

IceFire
  • 4,016
  • 2
  • 31
  • 51