Public Group

# newb problem with multi-d array (C++)

This topic is 3608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Building my own word jumble game. I get an error "syntax error {" and it refers to the multi-dimensional array. Code: Game.h
#include <string>

class Game
{
public:
Game();
virtual ~Game();

//randomly selects a word from the 2d array, and jumbles it.
void SetWord();
//returns the jumbled word
std::string GetWord();
private:
const std::string WORDS[6][2] =
{
{"rogue", "A stealthy type."},
{"karazhan", "A 10 man dungeon"},
{"mario", "A sidescroller ninja"},
{"darnassus", "Home of the night elves"},
{"shadowform", "31 talent point of a priest"},
{"northrend", "the new continent in wrath of the lich king"}
};
};


Game.cpp
#include "Game.h"
#include <ctime> //for random

Game::Game()
{

}

Game::~Game()
{
}

void Game::SetWord()
{
}

string Game::GetWord()
{
}


main.cpp
#include <iostream>
#include "Game.h"

int main()
{
Game* p_game = new Game();
}



##### Share on other sites
The problem is that you can initialize in the class only static integral constant member variables. In your case you can do something like the following:

#include <string>class Game{public:	Game();	virtual ~Game();		//randomly selects a word from the 2d array, and jumbles it.	void SetWord();	//returns the jumbled word	std::string GetWord();private:        /* make it static. */	static const std::string WORDS[6][2];};const std::string Game::WORDS[6][2] = 	{		{"rogue", "A stealthy type."},		{"karazhan", "A 10 man dungeon"},		{"mario", "A sidescroller ninja"},	    {"darnassus", "Home of the night elves"},	    {"shadowform", "31 talent point of a priest"},		{"northrend", "the new continent in wrath of the lich king"}	};

You can also write an inline function like the following:

const std::string &Game::Words(int i, int j){    const std::string WORDS[6][2] = 	{		{"rogue", "A stealthy type."},		{"karazhan", "A 10 man dungeon"},		{"mario", "A sidescroller ninja"},	    {"darnassus", "Home of the night elves"},	    {"shadowform", "31 talent point of a priest"},		{"northrend", "the new continent in wrath of the lich king"}	};    return WORDS[j];}

##### Share on other sites
Don't forget the std:: in GetWord() in Game.cpp. :)

##### Share on other sites
Also, rand() is in <cstdlib>. Maybe it's in <ctime>, too, but that's weird.

##### Share on other sites
Thanks for your help guys. I decided to use the function, as it serves a dual purpose. Just curious why the &Game::Words is a reference, I also had to write the &Game:: part in the function declaration as well.

Ocelot, you were right, I did need <cstdlib> as well.

##### Share on other sites
Quote:
 Original post by agm_ultimatexThanks for your help guys. I decided to use the function, as it serves a dual purpose. Just curious why the &Game::Words is a reference, I also had to write the &Game:: part in the function declaration as well.Ocelot, you were right, I did need as well.

I have written the & because the function returns a reference to the string. The & is part of the return type and not of the name of the function. You only need to have the Game:: part in the declaration of the function if it is outside of the class body.

• 16
• 9
• 13
• 41
• 15