Initializing an array of string type in a class

I want to use this array as part of my class. I have tried several different angles trying to get it to work but with out success. I have been checking to see if it works by simply using "cout << dayName[3];" It is printing nothing at all. can someone show me the proper way to initialize this array of strings?
First I tried this:
 
const string dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

then I tried this:
1
2
3
4
5
6
7
8
const string dayName[7]; 
dayName[0] = "Sunday";
dayName[1] = "Monday";
dayName[2] ="Tuesday";
dayName[3] ="Wednesday";
dayName[4] ="Thursday";
dayName[5] ="Friday";
dayName[6] ="Saturday";


My implemetation file code and my header file code is here (header first):

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
//dayType.h, the specification file for the class dayType
#include <iostream>
#include <string>
using namespace std;

class dayType{ 
private:
	
	
	string day; // To hold single instance of the name of a weekday.

	const string dayName[7]; // holds the names of the of the seven weekdays
		//"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"

	int dayNumber; // To hold an int representation of the location of a spicific day 
	    //within the array
	
	void setDay(); // Function to set the DayType variable "day" to the name of a 
		//weekday. This function recieves a call from promptUser() and begins by asking 
		//the user to enter the day to set. 
		//Postcondition: after the user enters the information in the form of a 
		//weekday name this function sets the day to that value.
public:

	void promptUser(); // Asks the user if they want to set the day and if yes 
		//prompts the user to set the day by entering the day name via function setDay.
		//Postcondition: If the user chooses to enter Y for yes when prompted this 
		//function calls setDay() 
	


Implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
//dayTypeImp
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;

//======================= dayType functions ====================================
dayType::dayType()
{
	const string dayName[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};
	int dayNumber = 0;
	string day; // To hold the string name of the day.
}
The problem is, you are declaring and initializing a new const string type in the constructor, which overrides the more global one(the class member).

So you must just initialize them straightaway--if possible.
Or make them non-const/const and give them the values in the constructor.

HTH,
Aceix.
Yea. It dosn't have to be a const. I tried initializing it with the values(day name strings) in the constructor only and the only other place dayName was mentioned was in the header. It was not a const when I tried that. This is what I had in the header:
string dayName[7];
and this is what I had in the constructor:
1
2
3
4
5
6
dayType::dayType()
{
	string dayName[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};
	int dayNumber = 0;
	string day; // To hold the string name of the day.
}


I still couldnt get it to cout<<dayName[2];
You're not listening to what Aceix told you.

The problem is that in your constructor you are declaring a new, local variable called dayName, and initializing the contents of that. You are not initializing the contents of the data member called dayName, because your local variable is hiding it.
1
2
3
4
5
6
7
8
9
10
struct day_type // in header day_type.h
{
    day_type() ;
    // ...

    private:
        static const std::string dayName[] ; // *** static (shared)
        int day_number ;
        // ...
};


1
2
3
4
5
6
7
8
9
10
/////////// in implementation day_type.cpp ////////////////

const std::string day_type::dayName[7] = // initialized here (not in the constructor)
       {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};

day_type::day_type()
{
    day_number = 0 ;
    // ...
}
Topic archived. No new replies allowed.