1

I have a struct called member. Within member I have a std::string array called months that I would like to initialize to default values. This is how I am doing it currently:

template <typenameT>                                                                                               
struct member                                                                                                       
{                                                                                                                   

    std::string months[12];                                                                                         
    std::string name;                                                                                               
    T hours_worked[12];                                                                                             
    T dues[12];                                                                                                     

    member() : months{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} {};    
};

However, whenever I compile I get this warning message:

warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x 

How can I do the initialization properly and get rid of this error message?

Edit:

I should of made my question more clear. I need compile this program on a older compiler and the -std=c++0x flag option will not be available to me. How can I do this correctly without using the flag.

3
  • 1
    It tells you exactly what to do, use -std=c++0x compilation flag. Commented Feb 29, 2012 at 8:56
  • @Als I would like to not have to set this flag. It seems like i am doing the initialization incorrectly. I have to run this on a different compiler where the -std=c++0x is not available. Commented Feb 29, 2012 at 8:58
  • i think this is a C++11 feature. your compiler must support C++11 for this code to work. Commented Feb 29, 2012 at 9:11

1 Answer 1

1

It tells you in the warning. Try adding -std=c++0x to your g++ arguments. If you want to be able to use this on an older compiler then you can't use initialiser lists the way you are doing.

Instead you could change member() to be something like

member()
{
  months[0] = "January";
  months[1] = "February";
  ...//etc
}
Sign up to request clarification or add additional context in comments.

2 Comments

I would like to not have to set this flag. It seems like i am doing the initialization incorrectly. I have to run this on a different compiler where the -std=c++0x is not available
Thank you the syntax on how to do this is what I needed.

Your Answer

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