![]() |
Newb interactive fiction
I'm trying to learn C++ and I started coding an interactive fiction game for practice and I can't get this to compile:
:
#include <iostream> |
Re: Newb interactive fiction
I can tell you have a poor understanding of variable definition scopes. To sum it up, when you define a variable inside a function (like currentRoom in main()), the variable is called Local (meaning that it is only defined inside that function). If you try to access it from another function, you will get a compiler error. If you have a need of that variable in a different function, you can pass it as an argument (just like you did with input).
Another problem I see is that Class Room is only defined AFTER the handle_input() function. This will create problems for you if you try to define a variable of type Room inside of that function. To get around this, you can either move the definition of Class Room and put it before handle_input(), or put a class prototype. |
Re: Newb interactive fiction
>> std::exit(0);
exit() is not in std namespace, so remove std:: because it will cause a compiler error. |
Re: Newb interactive fiction
using namespace std;
If you'll use the line in red after your preprocessor directives (#include statements), you won't have to type std:: before your statements. |
Re: Newb interactive fiction
Quote:
|
Re: Newb interactive fiction
Quote:
|
Re: Newb interactive fiction
Thanks for your help people. I coded python before and in python it seemed that only classes and objects had namespaces.
Is it a rule that you should usually define classes before anything else? People on ##c++@freenode said I shouldn't use "using namespace std" so I thought I'd trust them. If I do currentRoom=firstRoom then currentRoom is a copy of firstRoom? Should I use a pointer/reference instead? |
Re: Newb interactive fiction
>>Is it a rule that you should usually define classes before anything else?
Classes must be defined before they can be used or referenced. So the standard way to do it is to put class definitions in a header file so that the header file can be included in all of the *.cpp files that use it. >>People on ##c++@freenode said I shouldn't use "using namespace std" so I thought I'd trust them That is a generally accepted way to do it. Most programmers don't like the "using namespace std" because it brings everything from std namespace into the program. In very short programs it doesn't really matter so I use that too because it reduces typing std:: in front of everything. But in larger programs you will want to do something different, such as putting this after the includes: :
using std::cout; |
Re: Newb interactive fiction
I've seen some examples on msdn using the class header file this way also. If the header is name header.h then they add the line using namespace header.h and then they don't have to type their class name before class methods. I'm not sure how smart that is really, maybe somebody with more experience could shed some light.
|
Re: Newb interactive fiction
I tried to take it further but it's now even more f*d up. I have trouble understanding this pointer stuff.
:
#include <iostream> |
| All times are GMT -5. The time now is 9:55 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC