-
Ahhh I see. Well I understand at face value just struggling to get my head around the moving between rooms after the 1st move .
And no, not moving within the room, just between different rooms.
Dont think im advance neough to rite a program that does it like that, I will have to rite it all myself
-
Wait, I think im starting to get it, so that loop basicly continues for what ever room u are in? until the player quits? correct?..
Also, I have always used include, what is import?
-
It allows you to move between the rooms you have, yes.
So long as you have set up the proper room structs, it should work.
You could also set some pointers to NULL if, in a room, you can't move any more north, say. Remember to check for it also.
About the -> syntax. It is the same as "(*p).".
So basically it dereferences a pointer and access its members.
-
Import?
Don't worry about that, just use include. I copy-pasted out of my "Test" project I use for anything random like this, so there was other unrelated code. Otherwise you can actually compile this and have a working, simple, game.
-
What? No. Never include .cpp files.
Never, ever do anything like that. There is no import either.
.cpp files are compiled separately, thus they should not be included anywhere.
I don't know how many times I must repeat this.
-
Ahh I see, thanks :)
Code:
public:
string description;
Room *north;
Room *south;
Room *east;
Room *west;
};
// A very simple player class
class Player {
public:
Room *location;
};
int main (int argc, char **argv) {
// Create an example map with four rooms:
// For an actual game you might load this data from a file.
// Create four rooms.
Room *rooms = new Room [4];
rooms[0].description = "Room 1";
rooms[1].description = "Room 2";
rooms[2].description = "Room 3";
rooms[3].description = "Room 4";
With that part of the code, the dark red isnt a member, so surely the teal will be an error?
-
description is part of the class Room and you create 4 new rooms which has a description member, so it's fine.
And also, I would recommend that you avoid new. I don't really see a need for it.
We can also use a vector as:
Code:
std::vector<Room> Rooms;
Rooms.push_back( Room("Room 1") );
// etc
Of course, we'll need a constructor for Room, too, so here goes:
Code:
Room::Room(const std::string& Description): description(Description), north(NULL), south(NULL), east(NULL), west(NULL) { }
-
Well I just compiled the above and it says. . .
C:\Users\Administrator\Desktop\roomtesty.cpp|9|err or: `string' does not name a type|
C:\Users\Administrator\Desktop\roomtesty.cpp||In function `int main(int, char**)':|
C:\Users\Administrator\Desktop\roomtesty.cpp|30|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|31|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|32|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|33|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|61|er ror: `cout' was not declared in this scope|
C:\Users\Administrator\Desktop\roomtesty.cpp|61|er ror: `endl' was not declared in this scope|
C:\Users\Administrator\Desktop\roomtesty.cpp|62|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|64|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|66|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|68|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|70|er ror: 'class Room' has no member named 'description'|
C:\Users\Administrator\Desktop\roomtesty.cpp|75|er ror: `cin' was not declared in this scope|
||=== Build finished: 13 errors, 0 warnings ===|
Is it better to use a vector liek that?
-
You need to learn to interpret error messages. It says there is no such type as "string".
This is because it should be std::string (the example wasn't properly tested).
-
Ahh im so used to using namespace std, I didn't pick up why string was an error.
I think you mentioned to use std:: instead of using namespace std?
-
You can either do std::string or add using namespace std.
It is a good idea, however, to use std::, if only to get used to it.
-
How can I go about looking for runtime errors (I believe they are called). I am using vista and as soon as I run the exe which just contains the above example code, it comes up but then a dialog box appears ' roomexample.exe has stopped working'
-
Run it in a debugger. There are many of those.
My favorite would be Visual Studio, which is an IDE with a compiler and debugger among other things.
Of course, there's also GDB and other debuggers you can find on the web.
-
Probably a segmentation fault. Check your pointer logic.
-
Learning to use a debugger is also a very valuable tool that is very important to a programmer.
It also allows you to waste less time finding a problem and more fixing it.