FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C++ Using class methods in inline functions? Scope problem?





flatliner
Hi there, complete novice C++ programmer here, Having a bit of a problem using pointers to class methods inside functions. I think I said that right?
Basically I was having a problem were I would create an instance of a class I made called "pokemon", inside the main function. Then in another function called setup() I would use methods (SetName()) from the pokemon class to change members (variables) of the class pokemon, but once the setup() function returned the members of the class were destroyed/empty? So I was think something to do with scope so I tried using pointers and that like below but I not sure if I am doing it right.

The code below is just a snippet from a very larger function. Basically I get this error below.

Quote:
: error C2228: left of '.SetName' must have class/struct/union
type is 'pokemon *'
did you intend to use '->' instead?

Code:


int setup(pokemon * player1){

*player1.SetName(name);         // error is located on this line

return 1;
};



So I try *player->SetName(name); but it gave me this error

Quote:
error C2100: illegal indirection


Any ideas?
flatliner
Actually I just got it to work. I just tried the code below

Code:


int setup(pokemon * player1){

player1->SetName(name);         // error is located on this line

return 1;
};

Jamestf347
ehh i was going to say, you can't have the pointer not be classified like, *player1 it would have to be like .. char *player1
flatliner
Jamestf347 wrote:
ehh i was going to say, you can't have the pointer not be classified like, *player1 it would have to be like .. char *player1


but its already declared in the function parentheses, its of type "pokemon" its class I made.
I just making for practice a text-based game using the pokemon idea.
Jamestf347
Ooo okays Smile. I'm a bit confused about pointers myself, Highschool class doesn't go too indepth.
flatliner
Jamestf347 wrote:
Ooo okays Smile. I'm a bit confused about pointers myself, Highschool class doesn't go too indepth.

Yes they can be some what confusing. Well I would recommend you watch anitRTFM Youtube video series on C++ with something like 65+ videos from the ground up to advance features in C++. Its were I learnt most of my knowledge of C++. They are brilliant tutorials.

EDIT for a list of what each video is about go here http://cpptutorials.freeforums.org/video-index-t31.html
Jamestf347
Thank you Smile, I'll check them out after I get out of school and track.
fladry
I think these methods work well
Code:

int setup(pokemon * player1){

(*player1).SetName(name);         // error is located on this line

return 1;
};


Code:

int setup(pokemon * player1){

player1->SetName(name);         // error is located on this line

return 1;
};


But i would prefer using reference to pointer like
Code:

int setup(pokemon& player1){

player1.SetName(name);         // this is quite clean

return 1;
};
[/code]
Related topics
Just a suggestion
Partitions problem in XP
PHP OOP Tutorial
Boot up Windows XP 60 times faster
[c++]Using all the members of a class
C++, simple things???
Java tutorials
A little help on C++
C/C++
10 frih$ contest: what does this c++ code do?
Is faith necessary?
C++ An Inline Question
IN php what does the ON mean ->
c# ftp class
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.