1

I'm attempting to initialize an array of class objects across a .h and .cpp file. I initially declared it(game_map[12]) in the .h file as shown below:

    #include <string>

    using namespace std;

    class Game {

    public:
        Game();
        ~Game();
        void test();
        void InitializeMap(Game &game);

    private:
        class Room {
        public:
            Room (string desc);
            Room();
            ~Room();
            void PrintDesc(Room &current);
            void SetDirection(int array_index, Room &current);

            string description;
            static int adjacent[3];rooms.
            static string direction[4];
        };

static Room game_map[12]; //Here is my array declaration
};
.
.
.
.

However, when I try to initialize game_map in the implementation file...

#include "Game.h"

using namespace std;

/*Public members*/
Game::Game(){}
Game::~Game(){}
/*Private members*/

Room Game::game_map[12] = {Room("scary")}; //trying to initialize here
.
.
.
/*Room*/

int Game::Room::adjacent[] = {-1,-1,-1};
string Game::Room::direction[] = {"-1","-1","-1","-1"};

Game::Room::Room() {}

Game::Room::Room(string descript) {
    description = descript;
}


Game::Room::~Room() {}

 .
 .
 .

I get an error saying that Room is undefined, despite that fact that the Room constructor on the right hand side seems to be recognized. I've tried putting the declaration after the Room constructor, but that didn't fix the problem. Can someone tell me what is going on here?

Thanks!

1
  • 1
    Game::Room, not Room Commented Nov 16, 2013 at 15:45

2 Answers 2

3

Like this

Game::Room Game::game_map[12] = {Room("scary")}; //trying to initialize here

^^^^^^ add Game:: here

I don't like the look of all the static members however. Clear sign your design is wrong.

Sign up to request clarification or add additional context in comments.

8 Comments

What do you think would be a better way to accomplish what I'm trying to do? The only other way I can think of would be to pull the whole thing out and make it a global variable. Would that be better? I'm afraid I don't have a ton of experience yet.
@Adam I don't see what's wrong with just dropping the static.
Oh, I didn't know you could do that. Thanks!
Just tried straight deleting the static keyword, but that rendered the array inaccessible. I'm guessing its something to do with the way I've set up my visibility?
Not sure where you are trying to access it from. However if it isn't static you'll have to initialise it in the Game constructor, not as you are trying to do now.
|
1

I agree with the other comment by john abut using static but I did get the following to work, which I think is what you were after. I'll let you sort out the details on what needs to be private.

Below is how I set up the classes:

game.h:

#include "room.h"
class Game
{
public:
    Game();
    ~Game();
    void test();
    void InitializeMap(Game &game);
    static Room game_map[12];
};

game.cpp:

#include "game.h"
Game::Game() {}
Game::~Game() {}
Room Game::game_map[12] = {Room("scary")}; //initialize here

room.h:

#include <string>
class Room
{
public:
    Room (std::string desc);
    Room();
    ~Room();
    void PrintDesc(Room &current);
    void SetDirection(int array_index, Room &current);

    std::string description;
    static int adjacent[3];  // rooms.
    static std::string direction[4];
};

room.cpp:

#include "room.h"
Room::Room() {}
Room::~Room() {}
Room::Room(std::string descript) {
    description = descript;
}
int Room::adjacent[] = {-1,-1,-1};
std::string Room::direction[] = {"-1","-1","-1","-1"};

And then you can test it in main.cpp:

#include <iostream>
#include "game.h"
#include "room.h"

int main()
{
    Game aGame;
    Room *rooms = new Room[12];
    rooms = aGame.game_map;

    std::cout << rooms[0].description << std::endl;

    delete [] rooms;

    return 0;
}

With the output:

scary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.