Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum to unsigned int

Tags:

c++

enums

casting

I have the following code:

class mph {
public:
enum minute_periods {five, ten, fifteen, thirty}; 
   std::vector<minute_periods> factors;
  minute_periods fac;

void setUpFactors(void) { 
    factors.resize(4);
    factors[five] = 5;
    factors[ten] = 10;
    factors[fifteen] = 15;
    factors[thirty] = 30;
  }

and I get the following error:

error: invalid conversion from ‘int’ to ‘mph::minute_periods’

how do I fix it?

like image 609
user1155299 Avatar asked Oct 27 '25 02:10

user1155299


1 Answers

int's are not implicitly cast to enums, you have to do it explicitly which is why you're getting the error. I don't think this code will do what you want it to however, it looks like you want something like this.

    std::map<minute_periods, int> factors;

That way factors[five] will give you an int, and not an enum like the vector would. I'm guessing from your code this is what you want because the values 5, 10, 15 and 30 are outside your enum's range (which is from 0 to 3).

like image 88
SirGuy Avatar answered Oct 28 '25 16:10

SirGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!