initializing multidimensional string array

G'day all,

In one of my programs I have a 3x3 array of string that I use to display the outcome to rock, paper, scissors, and another 1x3 used for a number guessing game. I have the arrays declared in the header file as follows:
1
2
3
4
//Games.h
std::string rpsOutcome[3][3];

std::string hiLoOutcome[3];


and then initialized them in the cpp file:
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
//Games.cpp
string rpsOutcome[3][3]
{
		//row 1
	{ "Both of you picked rock, you tied. Try again",
		"You picked rock, the computer picked paper, you lose",
		"You picked rock, the computer picked scissors, you win"
	},
		//row 2
	{ "You picked paper, the computer picked rock, you win",
		"Both of you picked paper, you tied. Try again",
		"You picked paper, the computer picked scissors, you lose"
	},
		//row 3
	{ "You picked scissors, the computer picked rock, you lose",
		"You picked scissors, the computer picked paper, you win",
		"Both of you picked scissors, you tied. Try again" 
	}
};
	
string hiLoOutcome[3]
{
		"Correct, the number is",
		"Too low, guess again",
		"Too high, guess again"
};


From what I've read, Im pretty sure thats how your supposed to initialize multidimensional arrays (using the nested braces), but when I build the project, I get the following error:
1
2
3
4
5
6
7
8
9
10
11
12
1
1>  RockPaperScissors.cpp
1>  Games.cpp
1>c:\users\wuubb\google drive\visual studio projects\games\games\games.cpp(75): error C2374: 'games::rpsOutcome' : redefinition; multiple initialization
1>          c:\users\wuubb\google drive\visual studio projects\games\games\games.h(35) : see declaration of 'games::rpsOutcome'
1>c:\users\wuubb\google drive\visual studio projects\games\games\games.cpp(94): error C2374: 'games::hiLoOutcome' : redefinition; multiple initialization
1>          c:\users\wuubb\google drive\visual studio projects\games\games\games.h(37) : see declaration of 'games::hiLoOutcome'
1>  Generating Code...
1>  Compiling...
1>  Palindrome.cpp
1>  Main.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Could anyone help me figure out what I'm doing wrong? Any help is greatly appreciated.
Thanks mates!
1. Don't use arrays. Use a container class from the C++ Standard Library such as std::vector or std::array.
2. If you want to make your existing code work, you need an = between the ] and the {
1
2
3
4
//Games.h
std::string rpsOutcome[3][3];

std::string hiLoOutcome[3];
This does not just declares your variables. It is also defines them as global zero-initialized variables. And it conflicts with at least similar definition in Games.cpp

Declare variables as extern in your header:
1
2
extern std::string rpsOutcome[3][3];
extern std::string hiLoOutcome[3];


LB wrote:
If you want to make your existing code work, you need an = between the ] and the {
Not if C++11 enabled (uniform initialization)
1
2
3
4
//Games.h
std::string rpsOutcome[3][3];

std::string hiLoOutcome[3];

This does not just declares your variables. It is also defines them as global zero-initialized variables. And it conflicts with at least similar definition in Games.cpp

Declare variables as extern in your header:

1
2
extern std::string rpsOutcome[3][3];
extern std::string hiLoOutcome[3];



Thanks mate, didn't realize that arrays worked differently than the other variables. Should I just define them in the future and not declare them in my header files?
didn't realize that arrays worked differently than the other variables
It is the same with other variables.

Should I just define them in the future and not declare them in my header files?
No. Or else you would have compiler giveing you a multiole defunutuon error.
Topic archived. No new replies allowed.