|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hi, I'm fairly new to C++. As a programming friend puts it, "Ah, you're still learning to speak C." But I'm learning through setting goals and then trying to solve them. Currently, my self-appointed assignment is to develop a very basic text game. My prime focus is to really understand classes and exactly what I can do with them.
Here's the problem I'm having at the moment. I'm defining a class called "room." I want it to contain all the fundamental properties of a room in this text game, such as its description, exits, and so on. The description was easy. The exits are harder. I want the variable I'm using for the exits to be able to directly relate to their destination. My first idea was to make each room have a string variable called, "N_Exit" (for the North exit of the room) for instance, which I could later assign to be "Room_2". Let me say it in code, even though it didn't work. I'll cut out the extraneous stuff. Code:
class room
{
Public:
string N_Exit, S_Exit, E_Exit, W_Exit;
} room001, room002, room003;
Code:
room001.N_Exit = "room002"; room002.S_Exit = "room001"; To do this I created another object of class room, and designated it to contain the data of room001 (the starting room). Code:
room Current_Room; Current_Room = room001; But I'm having a devil of a time getting the Current_Room to change. This is the sequence I tried when the user inputs the command to travel North from room001 (into room002): Code:
if (usercommand = "n") and (Current_Room.N_Exit != "0")
{
Current_Room = Current_Room.N_Exit;
}
This seemed a little iffy to me, and indeed, the compiler rejected it. The deal is, the text string for Current_Room.N_Exit is valid text for an existing object of class room, so I thought maybe it would work. Ultimately, I guess my question boils down to how do I use the contents of a string to designate a class? The compiler doesn't like it when I mix types. It says Current_Room is a class, and Current_Room.N_Exit is a string, and can't make one of them equal another. I know this has GOT to be really basic, and all I need is the right syntax, so that's why it's a n00b question. But if I can't figure out the basic stuff, I'm not going to be able to do complex stuff later on. ![]() |
| Sponsored Links |
|
|
|
|||
|
Hm... I'm also trying to make a function that returns a string. It says
'string' does not name a type. Hm..... edit: Figured this part out. Just worry about my initial post... ![]() Last edited by MerakSpielman; 02-11-2008 at 03:42 PM. |
|
|||
|
Actually, it's not as basic as it seems. What it looks like you need to do is the following:
(1) Create a class (or struct) called Exit that contains two fields, a string called name and a pointer to a Room class. Put these in your Room class instead of the strings. Set the nextRoom pointer to null if you can't go that way. (2) When you input the direction, just use a simple if-else block to check which way the user wants to go, and then set the currRoom variable to the room pointer in your current room. For example: C++ Code:
Last edited by dargueta; 02-11-2008 at 10:35 PM. Reason: Fixed typo |
|
|||
|
Thanks, I'll give it a shot! I'll learn this yet...
edit: I wonder if I'll be able to figure out a grammar parser... lol edit again: a question about you code. I see the line Code:
Room *currRoom = new Room(); It looks like you're calling a Room like it was a function. How does that work? If you start in Room_001, for instance, wouldn't you just say Code:
Room *currRoom = Room_001; Last edited by MerakSpielman; 02-12-2008 at 01:20 AM. |
|
|||
|
No. currRoom is a pointer to a room, whereas Room_001 is a Room object itself. You have to do this with pointers, otherwise you'd have to copy the data members one by one. Are you familiar with constructors, destructors, and the like? If not, then look at these tutorials:
Classes I. Classes II - Advanced Stuff (You probably won't need this) Pointers Using new and delete |
| Sponsored Links |
|
|
|
|||
|
OK, I've now figured out constructors, deconstructors, and prototypes. Or at least I have a general idea how to declare them and get them doing what I want them to do, which will have to be good enough for the moment.
new and delete, those are new for me. I'm also getting the clue that the << and >> operators might be useful for me, too. I'm trying to get it so I can use user input to name new objects, so the player can input their own name and it will be saved as an object of class "player" for instance. I'm getting there, slowly but surely. Thanks for your help, dargueta. ![]() |
|
|||
|
You're quite welcome. Feel free to ask any more questions.
|
|
|||
|
aaaack!
I have a new situation that's driving me nuts. I've done as you suggested. But for some reason it's insisting my "exit" class doesn't exist. That is to say, class exit is declared before class room, but class room still can't see it. Code:
class room;
class exit
{
public:
string Exit_Name;
room *nextroom;
exit();
~exit();
};
class room
{
public:
string description;
exit Nexit;
exit NEexit;
exit Eexit;
exit SEexit;
exit Sexit;
exit SWexit;
exit Wexit;
exit NWexit;
exit Uexit;
exit Dexit;
room();
~room();
}Room_001, Room_002;
ISO C++ forbids declaration of `room' with no type So I figured I needed to declare "room" first, and stuck an empty "class room;" line above it. That successfully eliminated that error. (edit: though now I worry that it was created without the proper variables) My next errors are for each of the "exit" lines inside class room. Each one of them returns the compilation error: 'exit' does not define a type This is a different error than the other one, though grammatically it seems to be saying the same thing. But it's saying this when I have the class exit clearly defined right above it. In fact, no matter where in my code I try to define an object of class exit it returns the same error. I tried changing the name of class exit, in case 'exit' was somehow a reserved word, but it didn't help. A google search indicated that often this error occurs when "use namespace.std" is missing. But I have it, so that's not the problem either. edit: in case it matters, there are prototypes defined for both exit and room, directly underneath the code you see above, like so: Code:
room::room()
{};
exit::exit()
{};
Last edited by MerakSpielman; 02-14-2008 at 02:02 AM. Reason: added a bit more info |
|
|||
|
Odd... I swear I messed around with changing the spelling of the "exit" class, but it didn't work.
But on a whim I decided to capitalize it ("Exit"), and now it's compiling. Let's give it a whirl and see how it goes... |
|
|||||
|
exit is a pre-defined function in C++. You can't redefine it to be a class.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Instantiating a Class | John | PHP Tutorials | 0 | 09-23-2007 10:57 PM |
| Assignment | Lestat | C and C++ | 8 | 09-18-2007 03:20 PM |
| Issue writing to file: pointer to a class which contains pointers to other classes | Sheemer | C and C++ | 0 | 08-21-2007 02:17 AM |
| Class question in Delphi | tosh5457 | Pascal/Delphi | 4 | 06-11-2007 02:46 PM |
| Java Help Files | xXHalfSliceXx | Java Help | 3 | 11-29-2006 12:30 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |