nosknut
nosknut

Reputation: 51

C++: Applying Enums from inside a class, outside the class

class Stepper {
    public:
    enum microstepping {
        FULL,
        HALF,
        QUARTER
    };
    int stepsPerStep;

    Stepper(microstepping _microstepping) {
        switch(_microstepping){
        case FULL:
            stepsPerStep = 1;
            break;
        case HALF:
            stepsPerStep = 2;
            break;
        case QUARTER:
            stepsPerStep = 4;
            break;
        }
    }

};

void main() {
    Stepper stepperA(FULL);//Problem shows here

}

/* I am trying to use an enum to set an internal vareable in the specific instance of the class. The issue is that the enum is not recognized outside the class when i am trying to use it. Declaring the enum outside the class is not an option. */

Upvotes: 0

Views: 86

Answers (1)

Abhinav Gauniyal
Abhinav Gauniyal

Reputation: 7574

You need to qualify it with Stepper scope -

class Stepper {
    public:
    enum microstepping {
        FULL,
        HALF,
        QUARTER
    };
    int stepsPerStep;

    Stepper(microstepping _microstepping) {
        switch(_microstepping){
        case FULL:
            stepsPerStep = 1;
            break;
        case HALF:
            stepsPerStep = 2;
            break;
        case QUARTER:
            stepsPerStep = 4;
            break;
        }
    }

};

int main() {
    Stepper stepperA(Stepper::FULL);
}

and use enum class if possible -

class Stepper {
    public:
    enum class microstepping {
        FULL,
        HALF,
        QUARTER
    };
    int stepsPerStep;

    Stepper(microstepping _microstepping) {
        switch(_microstepping){
        case microstepping::FULL:
            stepsPerStep = 1;
            break;
        case microstepping::HALF:
            stepsPerStep = 2;
            break;
        case microstepping::QUARTER:
            stepsPerStep = 4;
            break;
        }
    }

};

int main() {
    Stepper stepperA(Stepper::microstepping::FULL);
}

Upvotes: 2

Related Questions