Member functions.

Hello.
Me and some mates thought that it would be good practice to write a text based rpg(pretty stupid idea maybe:P).
Anways the program uses alot of switch statments were the player has to make diffrent choices. We have encountered a problem which we need help with.
This is our main code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <iostream>
#include <string>
#include "Globals.h"
#include "Player.h"



using namespace std;

int main()
{
    Player Eon;// This is our problem, we need this declaration to be global
    string Name;
          cout << "Welcome to the Epic World of Woot" << endl;
          cout << "What is your name: ?" << endl;
          cin >> Name;
          Eon.setName(Name);
          cout << "Welcome "<< Eon.getName()<< endl;
          cout << "\n";
          lRoad(1); //Sends the player to road
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

/*Last change by K0nserv*/

The problem with this code is even though it works. The value of Eon isn't saved and can't be accessed. To make this a little easier to understand ill include the player class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cstdlib>
#include "Globals.h"

using namespace std;

class Player
{
      public:
             void setName(string a) {pName = a;}
             string getName() {return pName;}
      private:
              string pName;
}
                

I tried declaring Player Eon in globals.h but that just made a odd compiling loop, maybe a complete noob fault.
What we want to do if i understood it correctly is to to declear a member function in global scope.
Thanks in advance.
//k0nserv
Last edited on
It sounds to me like you simply want Eon to be available to functions outside of main(). There are a few ways to do this, but the quickest (and dirtiest) method is to declare it in your main.cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <string>
#include "Globals.h"
#include "Player.h"

using namespace std;

Player Eon; // Eon is now globally available to other functions

int main()
{
    string Name;
    cout << "Welcome to the Epic World of Woot" << endl;
    cout << "What is your name: ?" << endl;
    cin >> Name;
    Eon.setName(Name);
    cout << "Welcome "<< Eon.getName()<< endl;
    cout << "\n";
    lRoad(1); //Sends the player to road
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


I invite you and your mates to join up on another web site where a similar project is also underway: www.cppbeginners.com

Look under Projects / Text Based RPG
Try declaring Player Eon; before the main function.\

Edit: you beat me too it.
Last edited on
Okay.
But if it's declared before main() can it be accessed from lRoad() anothere function in anothere .cpp file ?
Hmm... I don't want to discourage you, but it's often best to learn to walk before you run, or you will definitely trip and fall flat on your face. I think it's awesome that you guys are working on a text based RPG, but if you're implementing classes without understanding more fundamental concepts like scope, you will quickly become frustrated.

Anyway... yes, you can have Eon available to other functions, but those functions have to refer to Eon as you declared it in your main .cpp file. You do that by using the extern keyword.

If this is your main .cpp file...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
. . .

#include "Globals.h"
#include "Player.h"

using namespace std;

Player Eon; // Eon is now globally available to other functions

int main()
{
    string Name;
    cout << "Welcome to the Epic World of Woot" << endl;

. . .


then your other cpp files have to understand that Eon is a variable (or object) of type Player that was declared elsewhere, and to use that declaration as the current declaration.

1
2
3
4
5
6
7
8
9
// someother.cpp that uses Eon
#include "Player.h"
void eon_test(void);

void eon_test(void)
{
  extern Player Eon;
  cout << Eon.getName() << endl;
}


Similarly, if you haven't declared these functions in an included header, you need to use the extern keyword to tell your main .cpp file that these functions exist.

While the above example will work, it is very poor programming, and I do not recommend you pursue this model. More ideally, you should write your functions so that they accept a parameter of reference to Player or pointer to Player, and then pass Eon to those functions. Not only is this more efficient, it is significantly less prone to error due to scope conflicts.
We have been looking in to Extern and tried using it but it for some reason generated an error code.
As for walking before running i agree, but i want to add that out of the 4 of us who are writing the program i am no the one with the most knowledge.
But we'll look in to pointers.
Last edited on
I encourage you to try out the beginner's web site: http://www.cppbeginners.com

There, you can post your whole project source, get other people to work on it with you, and get valuable feedback from people who are also interested in text based RPG.
Topic archived. No new replies allowed.