简体   繁体   中英

How do I make and fill and array of enum type

I am trying to make an array int indexed but I want the values to be of an specific enum type. The part I am having trouble with is how to fill the array without having to hard code it ie

int main(){
    enum Color{blue=0, green, brown,orange,black}
    Color colors[5];

    for(int i = blue; i!= black;i++){
        colors[i]=i;//this is the part the compiler does not like
    }


}
colors[i] = static_cast<Color>(i);

Note that your loop iterates four times and assigns four colors, indexed 0 through 3, blue through orange . colors[4] remains uninitialized.

一种解决方案是将循环计数器更改为Color而不是int

for(Color i = blue; i!= black;i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM