Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-11-2008, 12:47 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default n00b class question

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;
later on, I define the exits of those particular rooms.
Code:
room001.N_Exit = "room002";
room002.S_Exit = "room001";
See where I'm going here? I'm trying to make it so that when the user inputs that he wants to move north, the game will look at the N_Exit designation for the room he's currently in, and switch the current room designation to the value of N_Exit.

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;
It seemed to work, at least, when I input the command to show the room description of Current_Room, it outputted the description of room001. But maybe I should be using a pointer here? I don't quite understand when I should use pointers yet. Should Current_Room just be a pointer?

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;
  }
(I'm assigning "0" as the value for the exit if you can't go that way)

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 02-11-2008, 03:33 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-11-2008, 10:34 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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:
  1. class Room;
  2.  
  3. struct Exit
  4. {
  5.     string exitName;
  6.     Room *nextRoom;
  7. };
  8.  
  9. class Room
  10. {
  11. public:
  12.     Exit north;
  13.     Exit south;
  14.     Exit east;
  15.     Exit west;
  16.     //constructors and other methods here.
  17. };
  18.  
  19. int main(int argc, char **argv)
  20. {
  21.     Room *currRoom = new Room();
  22.     //other stuff, i.e. inputting commands, whatever.
  23.     if((usercommand == "n")&&(currRoom->north.nextRoom != NULL))
  24.         currRoom = currRoom->north.nextRoom;
  25.     else if((usercommand == "s")&&(currRoom->south.nextRoom != NULL))
  26.         currRoom = currRoom->south.nextRoom;
  27.     //etc.
  28. }

Last edited by dargueta; 02-11-2008 at 10:35 PM. Reason: Fixed typo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-12-2008, 01:03 AM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default

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();
I think I mentioned I'm a bit confused about pointers... I see you're declaring a pointer of class Room. I see that you're assigning it to point somewhere. But I don't understand the syntax of "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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-12-2008, 09:08 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 02-13-2008, 08:19 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-13-2008, 11:16 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

You're quite welcome. Feel free to ask any more questions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-14-2008, 01:54 AM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default

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;
If I don't include the first empty "class room;" line, I get the following error:
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-14-2008, 10:46 AM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default

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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-14-2008, 11:57 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 11:23 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads