Compiler error with array/string initialization

The compiler returns the following error when I try to compile any programs with an initialized string or array:
"extended initializer lists only available with -std=c++0x or -std=gnu++0x"

When I use the "-std" options, I would get something like this:
"invalid conversion from ‘const char*’ to ‘char’"

Below is the latest program I was working on that finally prompted me to post this question. The error is in the constructor on line 23; the initialization of month[]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <cstdlib>

using namespace std;

class DayOfYear
{
private:
    int julianDay;
    // UPDATE: previously "string month[12]".
    static const string month[12];

public:
    DayOfYear( int );
    void setJulianDay( int );
    void print();
};

// UPDATE: month is a static const and properly initialized per ne555 posts.
const string DayOfYear::month[12] = { "January", "February", "March", 
                                      "April", "May", "June", "July", 
                                      "August", "September", "October", 
                                      "November", "December" };

DayOfYear::DayOfYear( int day = 1 )
{
    julianDay = day;
/* WRONG
    month[12] = { "January", "February", "March", 
                  "April", "May", "June", "July", 
                  "August", "September", "October", 
                  "November", "December" };
*/
}

void DayOfYear::setJulianDay( int jd )
{
    if ( jd > 0 && jd < 365 )
	julianDay = jd;
    else
    {
	cout << "Invalid Julian Day. Terminating program...\n";
	exit(EXIT_FAILURE);
    }
}

void DayOfYear::print()
{
    if ( julianDay <= 31 ) // UPDATE: Fixed out of bounds subscripts as ne555 pointed out.
	cout << month[0] << " " << julianDay;
    else if ( julianDay <= 59 )
	cout << month[1] << " " << julianDay - 31;
    else if ( julianDay <= 90 )
	cout << month[2] << " " << julianDay - 59;
    else if ( julianDay <= 120 )
	cout << month[3] << " " << julianDay - 90;
    else if ( julianDay <= 151 )
	cout << month[4] << " " << julianDay - 120;
    else if ( julianDay <= 181 )
	cout << month[5] << " " << julianDay - 151;
    else if ( julianDay <= 212 )
	cout << month[6] << " " << julianDay - 181;
    else if ( julianDay <= 243 )
	cout << month[7] << " " << julianDay - 212;
    else if ( julianDay <= 273 )
	cout << month[8] << " " << julianDay - 243;
    else if ( julianDay <= 304 )
	cout << month[9] << " " << julianDay - 273;
    else if ( julianDay <= 334 )
	cout << month[10] << " " << julianDay - 304;
    else
	cout << month[11] << " " << julianDay - 334;
}

int main()
{
    int day;
    DayOfYear date;
    
    cout << "\nPlease enter a Julian Day: ";
    cin >> day;

    date.setJulianDay( day );
    date.print();
    cout << endl << endl;
    
    return 0;
}


I've tried using a the shell command "g++ program.cpp" and "g++ program.cpp -std=c++0x" as well as the gnu++0x option. I've also tried to compile the program with codeblocks. g++ and codeblocks return the same error.
Last edited on
1
2
string month[12]; //an array of 12 elements (strings)
month[12]; //one string. (actually is an invalid dereference, out of bounds) 


I think that the name of the months is common to all instances. So
1
2
3
4
5
class DayOfYear{
  static const string month[12];
};

const string DayOfYear::month[12] = {"Jan", "Feb", /**/};
Thank you ne55. That worked perfectly. I wish I realized that when I was breaking my head for a couple of hours trying to figure it out. Thanks for pointing out my out of bounds subscripts as well :)

Another question: You decided to declare the variable a const to protect its data from being change, but it could have worked just as well without it, right?

Also it seems a little contradicting to me that a const variable can't be altered after its declaration, but variables in a class can't be declared and initialized at the same time. I see how the initialization takes place after the class definition by making it static. Can you help me understand how is that possible with a constant?
I'm not sure if I understand you.
You set the value of constants in the constructor.
1
2
3
4
5
class foo{
  const int some_constant;
public:
  foo(int n): some_constant(n){} //initialization list
};
Thank you for the extra information. I'm still new to the subject, but I'm sure I'll fully grasp it in time. Your replies certainly helped me understand it much better.
Topic archived. No new replies allowed.