Defining an object from another file that's not in the global scope.

I have 4 files, but I think you only need two to help me. Basically I have a vector full of "monsters" (std::vector<Monster> Monsters) in Rooms.h/cpp. I have the monster class in Monsters.h/cpp. I think you know where I'm going with this.

Basically, I create the monsters in a static void inside the monster class (static void MonsterSetup();). I think looking at it will show you what I mean. As you could think, Monsters.push_back(Butterfly); is undefined.

Rooms.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include "Rooms.h"
#include "Monsters.h"



void Room::RoomSetup()
{
	Room Wildneress("The place of all places to practice!");
	Wildneress.Monsters.push_back(Butterfly);


}


Monsters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Monster::Monster(int newId, string newName, string newInfo, string newType, int newHealth, int newDamage, int newGold, int newXp)
{
	id = newId;
	name = newName;
	info = newInfo;
	type = newType;
	health = newHealth;
	damage = newDamage;
	gold = newGold;
	xp = newXp;
}

void Monster::MonsterSetup()
{
	//id, name, info, type, health, damage, gold, xp
	static Monster Butterfly(1, "A butterfly",  "A cute butterfly.. you monster", "Normal", 1, 1, 0, 10);



}


I know allabout paramaters, but I think I heard somewhere it's not the best choice/you can't with objects or classes.

Thanks a lot of helping! Sorry that it's so long. Maybe I gave too much info.
The problem is the scope of Butterfly is inside that function, even though it is static it is not visible to anything outside of that function.
What you need to do is put static Monster Butterfly;
inside the public section of the Monster class (inside h-file) and at the top of the Monsters.cpp file define space for your class (static) variables.

Monster::Monster Butterfly(1, "A butterfly", "A cute butterfly.. you monster", "Normal", 1, 1, 0, 10);

Another problem I see is inside Room::RoomSetup()
Wildneress is on the stack so when you return from this method it will be gone.
Thanks a lot! What do you mean "on the stack"?
Er, I'm confused. Do this?

Monster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Monster
{
public:
	Monster(int newId, string newName, string newInfo, string newType, int newHealth, int newDamage, int newGold, int newXp);
	~Monster();
	string name;
	string info;
	string type;

	unsigned int id;
	unsigned int health;
	unsigned int damage;
	unsigned int gold;
	unsigned int xp;


	static void MonsterSetup();



	static Monster mButterfly;
	
	
};


Monsters.cpp
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include "Monsters.h"

Monster mButterfly(1, "A butterfly",  "A cute butterfly.. you monster", "Normal", 1, 1, 0, 10);

Monster::Monster(int newId, string newName, string newInfo, string newType, int newHealth, int newDamage, int newGold, int newXp)
{
	id = newId;
	name = newName;
	info = newInfo;
	type = newType;
	health = newHealth;
	damage = newDamage;
	gold = newGold;
	xp = newXp;
}

void Monster::MonsterSetup()
{
	//id, name, info, type, health, damage, gold, xp
	static Monster mButterfly(1, "A butterfly",  "A cute butterfly.. you monster", "Normal", 1, 1, 0, 10);



}


It doesn't seem to work.
Topic archived. No new replies allowed.