0

I'm a beginner in C++ so I'm sorry if this is super obvious, but I have an issue with assigning values to an enum. I've declared the enum like so in a header file:

 enum face
    {   paramControlHeight = 40,
        paramLabelWidth    = 80,
        paramSliderWidth   = 300
    };

And tried assigning an integer. Needless to say it doesn't work:

paramControlHeight = 40;//Not assignable

After googling about for a while, I tried:

using type_of_p=decltype(paramControlHeight);

Which as I understand, should yield the type of paramControlHeight, and enable me to use

paramControlHeight=static_cast<type_of_p> (40);

But I get the same "un-assignable" error

If anyone could point me in the right direction I'd be very grateful

7
  • 2
    What do you mean by "assign values to enum"? Do you want to create a variable of type face and assign value 40 to it? Commented Feb 27, 2020 at 20:52
  • I want to assign "paramControlHeight" which is inside my enum a different value. So, for example, it starts out as 40, but I would like to change it to 80 later on . Commented Feb 27, 2020 at 20:54
  • enums are contant expressions and cannot be changed. Commented Feb 27, 2020 at 20:55
  • paramControlHeight is a constant. You assign variables with it: auto var = paramControlHeight; You can't assign new values to a constant. Commented Feb 27, 2020 at 20:55
  • That's not possible. Enums have set values. You need a variable (e.g. int paramControlHeight = 40) to be able to change it. Commented Feb 27, 2020 at 20:55

2 Answers 2

2

I want to assign "paramControlHeight" which is inside my enum a different value. So, for example, it starts out as 40, but I would like to change it to 80 later on

You seem to misunderstand what enums are. You seem to expect the enum to behave like this

struct face
{   int paramControlHeight = 40;
    int paramLabelWidth    = 80;
    int paramSliderWidth   = 300;
};

face f;                     // create instance
f.paramControlHeight = 40;  // modify member

However, an enum is rather like a

struct face
{   
    static const int paramControlHeight = 40;
    static const int paramLabelWidth    = 80;
    static const int paramSliderWidth   = 300;
};

Now back to your actual enum:

enum face
{   paramControlHeight = 40,
    paramLabelWidth    = 80,
    paramSliderWidth   = 300
};

Here paramControlHeight is an enumerator with the value 40. You cannot modify it. It is not meant to be modified. It is meant to enumerate. What you can do is:

face f{ paramControlHeight };   // create instance of face
f = paramSliderWidth;           // assign a different value to it

A more typical enum would be

enum face_parts {
    nose = 1,
    eye = 2,
    mouth = 3
};

That you could use like this

void print_face_part( face_parts fp ){
    if (fp == nose) std::cout << "nose";
    if (fp == eye) std::cout << "eye";
    if (fp == mouth) std::cout << "mouth";
}

In simple terms an enum lets you name and group constants. Note that since C++11 there are scoped enums that are more flexible and dont introduce the name of the enumerators in the enclosing namespace.

Sign up to request clarification or add additional context in comments.

Comments

1

paramControlHeight, paramLabelWidth, paramSliderWidth are the values. You can't assign anything to them more than you can assign a value to 42.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.