String in class

Hi, I'm trying to make a class called dayType. As a private member, I declared and initialized a string array to hold the days of the week and for some reason, my compiler doesn't like it. Here are the errors.
(17) syntax error : '{'
(17) unexpected token(s) preceding '{'; skipping apparent function body
error C2065: 'dayList' : undeclared identifier... (got 8 of these errors)
Is it because I'm including a class (string) in my own class? I am not sure what to do about that problem however. Here is my code.

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
#include <iostream>
#include <string>

using namespace std;

class dayType
{
public:
	void setDay(string dayOfWeek);
	void printDay() const; 
	string returnDay() const;

	string returnNextDay() const;
	string returnPreviousDay() const;
	string returnDayByAdding(int num);
private:
	string dayList[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
	int numberOfDay; //variable to hold the index of array dayList
};

int main()
{
	

	return 0;
}

void dayType::setDay(string dayOfWeek)
{
	for(int index=0;index<7;index++)
		if(dayList[index]==dayOfWeek)
		{
			numberOfDay=index;
			break;
		}
}

void dayType::printDay() const
{
	cout << dayList[numberOfDay] << endl;
}

string dayType::returnDay() const
{
	return dayList[numberOfDay];
}

string dayType::returnNextDay() const
{
	return dayList[numberOfDay+1];
}

string dayType::returnPreviousDay() const
{
	return dayList[numberOfDay-1];
}

string dayType::returnDayByAdding(int num)
{
	int tempDay=numberOfDay+num;
	numberOfDay=tempDay%7;
	return dayList[numberOfDay];
}
You can't assign to non-const, non-static integral members inside a class body. Initialize your array inside the constructor.
Ok thanks. Can't believe I missed that.
Now that I consider it, that array probably should be static and constant anyway. The values of the days of the week shouldn't change and ought to be the same for all classes, I would think.
Ok so here is my new code (that works!)

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
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>

using namespace std;

class dayType
{
public:
	void setDay(string dayOfWeek);
	void printDay() const; 
	string returnDay() const;
	string returnNextDay() const;
	string returnPreviousDay() const;
	string returnDayByAdding(int num);
	dayType();
private:
	string dayList[7];
	int numberOfDay; //variable to hold the index of array dayList
};

int main()
{
	dayType myWeek;
	string weekDay;
	int increaseDay;
	
	cout << "Today is ";
	myWeek.printDay();
	
	cout << "Enter the day you would like to begin with.";
	cin >> weekDay;
	myWeek.setDay(weekDay);

	cout << "After setting the day to your desired day, the day is ";
	myWeek.printDay();

	cout << "The day of the week is " << myWeek.returnDay() << endl;
	cout << "The next day is " << myWeek.returnNextDay() << endl;
	cout << "Yesterday was " << myWeek.returnPreviousDay() << endl;

	cout << "Enter the number of days to increase.";
	cin >> increaseDay;

	cout << "After " << increaseDay << " days, the day of the week is " << myWeek.returnDayByAdding(increaseDay) << endl;


	return 0;
}

void dayType::setDay(string dayOfWeek)
{
	for(int index=0;index<7;index++)
		if(dayList[index]==dayOfWeek)
		{
			numberOfDay=index;
			break;
		}
}

void dayType::printDay() const
{
	cout << dayList[numberOfDay] << endl;
}

string dayType::returnDay() const
{
	return dayList[numberOfDay];
}

string dayType::returnNextDay() const
{
	return dayList[numberOfDay+1];
}

string dayType::returnPreviousDay() const
{
	return dayList[numberOfDay-1];
}

string dayType::returnDayByAdding(int num)
{
	int tempDay=numberOfDay+num;
	numberOfDay=tempDay%7;
	return dayList[numberOfDay];
}

dayType::dayType()
{
	dayList[0]="Sunday";
	dayList[1]="Monday";
	dayList[2]="Tuesday";
	dayList[3]="Wednesday";
	dayList[4]="Thursday";
	dayList[5]="Friday";
	dayList[6]="Saturday";
	numberOfDay=0;
}


dayList should be static, like Zhuge said.
It makes no sense for each class instance to have its own copy of that array.
Yeah, the next section in my book discusses static members. Once I read and study that, I'm going to configure this program to accomodate that.
Topic archived. No new replies allowed.