Advertisement

returning pointers

Started by July 06, 2000 12:41 PM
5 comments, last by Joker2000 24 years, 5 months ago
Ok, I''ve seen this method being used before (a function having a return type of a pointer) so I don''t know why it shouldn''t work in this situation. However, it generates a "declaration syntax" error: // funtion prototype player * create_new_player(void); That should work, right? I''ve tried moving the asterisk to be right after player like: player* create_new_player(void); and also player *create_new_player(void); but none have worked. Hopefully this will be like my "strings" post and I''ll get 500 replies in about two minutes. Also, this is not going to be my only problem with this pointer stuff. I''m getting lots of other errors, no doubt, but I''m hoping that solving just one or two of them will fix the rest. Thanks. Joker
is ''player'' defined at this point?

What type of error are you getting? (actual error string)

--------------------------
Carpe Diem
D.V.Carpe Diem
Advertisement
Would you please post the class declaration and possibly the rest of the code if it''s not too long. Thanks!

..-=ViKtOr=-..
Ok, declaring the globals before the function prototypes fixed that problem. However, I still get the following error:

// globals
class player;
class weapon;

// function prototypes
player * create_new_player();

// the player class
class player
{
public:
player(string, int, int, int, int, int);
~player(void);
void show_player_stats(void);
private:
string player_name;
int weapon_status;
int health;
int armor;
int money;
int experience;
};

// beginning of actual code
void prompt_new_player(void)
{
char menu_choice;
cout << endl << "[C]reate New Player\n[L]oad Saved Player";
menu_choice = getch();
if (menu_choice == 'c')
player * pPlayer = create_new_player();
}

void main(void)
{
prompt_new_player();
}

player * create_new_player(void)
{
string name;

cout << endl << "What is the name of your new player?\n";
getline(cin, name);
player * pPointer = new player(name,0,100,0,500,0);
return pPointer;
cout << "Created New Player: " << name;
pPlayer->show_player_stats(); // <----- ERROR HERE
}

void player::show_player_stats(void)
{
cout << player_name << weapon_status << etc. etc. etc.
}

player::player(string p_name, int w, int h, int a, int m, int e)
{
this->player_name = p_name;
this->weapon_status = w;
this->health = h;
this->armor = a;
this->money = m;
this->experience = e;
}

void menu_main(void)
{
char menu;
// I have lots of code here but this next line is all that matters
cout << "Press S to see player stats.";
menu = getch();
if (menu == 'S')
pPlayer.show_player_stats(); // SEE NOTE 1 AT BOTTOM
}

The error I'm getting on:
pPlayer->show_player_stats();
is "undefined symbol pPlayer".

NOTE 1: This pretty much the only reason I'm having a problem. Because the player object is created in another function, the menu_main function has no way of knowing pPlayer exists and therefore I can't call the stats function from the main menu.

I think this is all the relevant code, everything else doesn't really apply. By the way, even the code I've written here has been "dumbed down". I have better error checking routines (valid menu input, etc.) that I didn't put in because of space so don't worry about those things.


Edited by - Joker2000 on July 6, 2000 2:50:27 PM
The problem is that you are using "player" before it's defined. Put the class definition of "player" up top.

Edited by - Buster on July 6, 2000 2:46:52 PM


void menu_main(player *DaPlayer)
{
char menu;
// I have lots of code here but this next line is all that matters
cout << "Press S to see player stats.";
menu = getch();
if (menu == ''S'')
DaPlayer->show_player_stats(); // SEE NOTE 1 AT BOTTOM
}

now when you call menu_main() call it like this...

menu_main(pPlayer);

hope this helps!

..-=ViKtOr=-..
Advertisement
quote: Original post by Joker2000

Ok, declaring the globals before the function prototypes fixed that problem. However, I still get the following error:

// globals
class player;
class weapon;


No, they aren''t globals. Globals are instances of classes. These are just forward declarations of classes.

quote:

// function prototypes
player * create_new_player();



create_new_player() should be a static method of the player class, but that''s a style issue and won''t cause you any errors.

quote:
player * create_new_player(void)
{
string name;

cout << endl << "What is the name of your new player?\n";
getline(cin, name);
player * pPointer = new player(name,0,100,0,500,0);
return pPointer;
cout << "Created New Player: " << name;
pPlayer->show_player_stats(); // <----- ERROR HERE
}


Problem 1: the pointer you''re using is called ''pPointer'', not ''pPlayer''. So pPlayer is invalid here. You use it in another function, but that''s irrelevant: the one you''ve got here is called pPointer.

Problem 2: the ''return pPointer'' ends execution of your function. The cout line and the show_player_stats() line will never get called.


quote:
player::player(string p_name, int w, int h, int a, int m, int e)
{
this->player_name = p_name;
this->weapon_status = w;
this->health = h;
this->armor = a;
this->money = m;
this->experience = e;
}


There''s no need to explicitly use the ''this'' pointer, as the class''s member variables are automatically in scope, so you could do this instead:

player::player(string p_name, int w, int h, int a, int m, int e)
{
player_name = p_name;
weapon_status = w;
health = h;
armor = a;
money = m;
experience = e;
}

A preferable way of doing this would be to use the constructor initialiser list, like so:
player::player(string p_name, int w, int h, int a, int m, int e)
: player_name(p_name), weapon_status(w), health(h), armor(a), money(m), experience(e)
{}

This will work out quicker when your classes contain instances of other classes and is a good habit to get into.


I think you are misunderstanding how variable scope and lifetime works. A variable you declare in a function disappears when the function ends. The only way to get information back from a function is through returning it. Your call to prompt_new_player() creates a pointer called pPlayer, allocates a new player to it, then that pointer disappears forever at the end of the function. So you can''t use it elsewhere.

I would recommend picking up a good programming book, as these things are generally described in some detail. A pointer is just the same as any other variable, in that declaring one within a block means it''s gone at the end of the block, by the way.

This topic is closed to new replies.

Advertisement