Skip to main content
gamedev.net
🔒 Locked

problem with classes?

Started by laotzu 5 replies 350+ views
Original Post
laotzu
laotzu
I'm still a bit of a beginner and still messing around with text games but anyways, right now I'm having a problem with two of my classes, where the top of my file right now looks like ... #include "creatures.cpp" creature creatures[NUMBER_OF_CREATURES]; #include "rooms.cpp" room rooms[NUMBER_OF_ROOMS]; ... now, the problem is that both of these use each other, so since creatures comes first it gives me the error that rooms isn't declared yet. Is there a why to tell the compiler that rooms is coming up? or any help would be great. Thank you Edited by - laotzu on 10/18/00 3:15:07 PM Edited by - laotzu on 10/18/00 3:27:37 PM
Tornado
Tornado
Try something like this:

#include "creatures.cpp"
#include "rooms.cpp"
class creature;
class room;
creature creatures[NUMBER_OF_CREATURES];
room rooms[NUMBER_OF_ROOMS];

EDIT: And why don't you use headers?

- Goblineye Entertainment
The road to success is always under construction

Edited by - Tornado on October 18, 2000 4:21:51 PM
Goblineye EntertainmentThe road to success is always under construction
bbxx
bbxx
You need to use header files.
Matt Teiken
laotzu
laotzu
Ok, with Tornado''s I still get the problem because the instances are actually trying to be used inside the files, so then their both undeclared.

And I am using header files their just called within the seperate files,
e.g. the top of creatures.cpp has
#include "creatures.h"
laotzu
laotzu
ooh.. ooh.. it''s almost working, thank you guys helped see that I was doing it weird...

took the header files outside of those files and added them above, like

#include "creatures.h"
#include "rooms.h"
#include "creatures.cpp"
....

and then added extern room rooms[NUMBER_OF_ROOMS]; to creatures.cpp
and now it''s almost working... I''m not sure if the new problem is related to that, but thank you two.
Z01
Z01
First, make sure you have all your class declarations, defines, constants, prototypes in your header files.

If you have the files
creatures.h
creatures.cpp
rooms.h
rooms.cpp
gamemain.cpp

These are the only includes you would need:
In creatures.cpp
#include "creatures.h"
#include "rooms.h"

In rooms.cpp
#include "rooms.h"
#include "creatures.h"

In winmain.cpp
#include "rooms.h"
#include "creatures.h"

After you''ve done this, add all the .cpp and .h files into your main work space. This is important.

Make sure your headers have inclusion guards. Also, the first thing in your .cpp file should be the #include''s. Don''t put in any code until after all the #includes.

If you have correctly separated interface from implementation by putting only the interface in the header file you should have no problem.

Good luck!
laotzu
laotzu
oh bother, it compiles now, but it hangs at the beginning of the program,

all the info is saved in data files so at the beginning of main
it goes something like

In.open("creatures.data");
//loop that reads in info
In.close

//That part works fine, then there''s the room one which use to work, and is exactly the same as creatures (except for its rooms

Now I''m guessing it has to do with that extern I''m using..?
I have to get to a class right now so I don''t have time to try your suggestion until later tonight, but thank you Z01.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.