Jump to content
  • Advertisement
Sign in to follow this  
agm_ultimatex

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.

If you intended to correct an error in the post then please contact us.

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();
}

Thanks for your help :)

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by agm_ultimatex
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.


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!