Where to put many game objects

I apologise if this seems like more than one question but...

In my rpg text adventure project, I'm having trouble knowing where to put all the game objects.

I've got a Location class and a Weapons class, and I was going to create the weapon objects so that their positions can be linked to the locations. This led me to think I had to create ALL the weapon objects, of which there may be dozens, which will be quite a lot of data, with each one having it's own data members with descriptions etc.

What I wanted to do was instantiate all the objects in a separate file. I thought it could go in the Weapon.cpp file after the class stuff, but my main .cpp can't acces the data. This may be the completely wrong way of doing it, so I'm all ears!

So
1. Can you only put function declarations and definitions in .h and .cpp files? If yes, then where do I put all the non function objects if I don't want them in my main.cpp file?

2. If I can put them in, then how does it work? I've included everything in the right places, but I just get undefined identifier errors. I thought that if the files are include in main.cpp, then they are as good as being actually in the same file.

As a side note, I am very new to c++, so am only using things I currently understand, but I am deliberately making the game complex because it's the way I learn the best :)

This is an example of what I was trying to do, not my actual code. In main on line 18, rat2 is undefined, even though the file it's defined in is included.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  // Pet.h

#pragma once

class Pet
{
private:
	int m_age;
	string m_colour;
public:
	Pet(int, string);

	int Age();
	string Colour();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   // Pet.cpp

#include "stdafx.h"
#include "Pet.h"



Pet::Pet(int age, string colour) :
	m_age(age),
	m_colour(colour)
{}
int Pet::Age() { return m_age; };
string Pet::Colour() { return m_colour; };


Pet rat2(1, "black");	// main.cpp can't see this.  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   // main.cpp


#include "Pet.h"
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

//Pet rat2(1, "black");		// This works if un-commented

int main()
{
	Pet rat(2, "white");

	cout << rat.Colour << endl;
	cout << rat2.Colour << endl;	// doesnt work, "rat2" unidentified

    return 0;
}
you can force it to see rat2 with the extern keyword, but that is C-ish and I don't think it is best.

what I think you want is a container of pets.
eg
vector<Pet> rat {{2,"white"},{1,"black"}}; //2 rats
cout << rat[0].Colour; //rat1, etc

I did have a bit of a play with arrays and vectors, but it still leaves the question of where do you put the vector if you don't want it in your main.cpp?

The actual weapon objects will have at least 10 parameters, including different descriptions to allow for different conditional circumstances, so for all the objects there will be god knows how may pages data.
One option would be create a Game class to hold everything game related together.
Inside the Game you can have a vector of Locations. Inside the Location class you store the weapons at this location.
another option is to have the actual data in a file, and read it in. That way you can change the locations or whatever without actually changing the code or recompile etc.

if you want to keep hard-coding it inside the program a class that just wraps it up and populates the data structures is fine.

You can also organize the code simply with #includes, though this is a little crude and not recommended when there is a better way (its sometimes used to force inline a routine that the compiler refuses to inline for you).

eg

file.cpp

int x,y,z;
object a,b,c;

main()
{
#include file.cpp //this drops the variable contents of file.cpp right here but gets it out of the way
...
}
@Thomas
I probably will end up with it structured as you said in a game class. Although I can't decide whether to store the weapon locations in the locations or in the weapons themselves.

@jonnin
Having a data file seems like the sort of thing I was thinking of, but I haven't done anything like that yet so no idea where to begin. I'm tempted to just drop a .cpp file in, but I didn't think was "proper". It would at least do what I wanted.

Thanks for the replies guys :)
Put this program down and use google for a few min or look in these forums and youll quickly be able to learn enough to read and write a simple data file. Then you can add it to your program and be much better off.

The #include trick is legit (its valid to do in the language) but like goto, its an example of what not to do unless you have a very good reason. There are better ways to break up large chunks of code, and learning those will do you better in the long run.
Topic archived. No new replies allowed.