ndarkness
ndarkness

Reputation: 1099

Protected enum not seen as type, why?

I am compiling some code that defines an emun as protected, like this

class MPU9250
{
  protected:
    // Set initial input parameters
    enum A_scale
    {
      AFS_2G = 0,
      AFS_4G,
      AFS_8G,
      AFS_16G
    };
}

I would like to use that enum in my method like this, this method is now public within the same class

void setAccScale(A_scale accScale);

However while compiling error saying

../../MPU9250.h: In function 'void initMovementDetected()':
../../MPU9250.h:196:7: error: 'MPU9250::A_scale AFS_8G' is protected
       AFS_8G,
       ^
../../nRF52.cpp:49:31: error: within this context
  mpu9250.setAccScale(MPU9250::AFS_8G);

Why is this? May I setAccScale public?

Thanks

class MPU9250
{
  protected:
    // Set initial input parameters
    enum A_scale
    {
      AFS_2G = 0,
      AFS_4G,
      AFS_8G,
      AFS_16G
    };
 public:
       void setAccScale(A_scale accScale);

}

EDIT: I found partially the error, there was a variable called Ascale as well, I renamed the enum to A_scale

Upvotes: 0

Views: 578

Answers (3)

user2622016
user2622016

Reputation: 6423

Yes, if you are using MPU9250::A_scale from outside of this or derived class, make A_scale public.

But since void setAccScale(A_scale accScale) is already private, there is no scenario when A_scale need to be used publicly. Could you show exactly where it is called, some more context:

../../nRF52.cpp:49:31: error: within this context
  mpu9250.setAccScale(MPU9250::AFS_8G);

Upvotes: 0

mrogal.ski
mrogal.ski

Reputation: 5940

Judging by your error :

../../MPU9250.h: In function 'void initMovementDetected()':
../../MPU9250.h:196:7: error: 'MPU9250::A_scale AFS_8G' is protected AFS_8G,
^
../../nRF52.cpp:49:31: error: within this context
mpu9250.setAccScale(MPU9250::AFS_8G);

And your data structure :

class MPU9250
{
protected:
    enum A_scale
    {
      AFS_2G = 0,
      AFS_4G,
      AFS_8G,
      AFS_16G
    };

private:
    void setAccScale(A_Scale);
}

I can assume that you're trying to call MPU9250::setAccScale from outside of the MPU9250 class which is impossible because of the accessibility of that function.
Another thing is that you're trying to access enumeration that is protected subtype of your MPU9250.

For the fix I would suggest making your A_scale publicly visible or rewrite this to use #define:

Solution 1 :

// define this in some header
#ifndef AFS_2G
#    define AFS_2G 0
#endif

#ifndef AFS_4G
#    define AFS_4G 1
#endif
// rest of your values ...

//to use this:
mpu9250.SetAccScale(AFS_2G);

Solution 2 :

//declare this enum as public :
class MPU9250
{
public:
    enum A_scale
    {
        AFS_2G = 0,
        AFS_4G = 1
        // rest of your values
    }
};

// to use this:
mpu9250.SetAccScale(MPU9250::A_scale::AFS_2G);

Both of the solutions require from you to make a public function SetAccScale because as I assume you're setting this scale from outside of MPU9250 type.

Upvotes: 1

Paul Isaac
Paul Isaac

Reputation: 84

I think you need a public class if you're gonna use enum outside of that scope. Also, you need an argument in your function and are missing a semi-colon. I am not sure why you are declaring a function inside of a class? But here is your code with those corrections. Hopefully, I was able to help you.

#include "stdafx.h"
#include <iostream>

class MPU9250
{
public:
    // Set initial input parameters
    enum Ascale
    {
        AFS_2G = 0,
        AFS_4G,
        AFS_8G,
        AFS_16G
    };
    void setAccScale(Ascale accScale) {
        std::cout << accScale << std::endl;
    } // function needs argument

};// missing semi-colon
int main() {
    return 0;
}

Upvotes: 1

Related Questions