Page 1 of 2
[Solved] Calling a class function from another class
Posted: Sat Jun 13, 2009 4:48 pm
by hurstshifter
Hey Everyone,
So basically I'm trying to call a function from one class in a function from another class. Here's a snippet of the code. I have never done this before and am realizing quickly that it isn't as simple as it sounds.
Code: Select all
void bullets::createbullets()
{
SDL_Rect pTemp = player::getprect();
bulletRect.x = pTemp.x;
bulletRect.y = (pTemp.y - 10);
bDead = 1;
bVel = 1;
}
SDL_Rect player::getprect()
{
return p;
}
I'm effectively returning the SDL_Rect of the player into a temporary rect in the bullet function to be used to map the location of the bullet correctly onto the screen. The error I get upon compilation is "Illegal call of non-static member function". Sorry if this is a beginners mistake.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 5:05 pm
by Lucas
In the bullet::createbullets() function could you not just get the x, y of the player as you normally would?
For example, since you must have created an instance of the player somewhere, for the bullets x, y could you not just say something like:
Code: Select all
x = PlayerInstance.get_x();
y = PlayerInstance.get_y();
or now that I'm thinking about it a little more instead of hard coding the players instance into the class function like that you could just pass the function a pointer to your player instance.
That way you wouldn't be limited to only creating bullets at the player.
Code: Select all
void bullets::createbullets(player *PlayerInstance)
{
x = PlayerInstance->get_x();
y = PlayerInstance->get_y();
}
I hope that made sense, and I hope my code is ok, my thoughts are in the right place I'm just having trouble putting them into words :P
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 5:11 pm
by Innerscope
I'd probably have an array of bullet objects as a member of the player class. That way you can initialize/set them within player, without having to use something like a delegate.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 6:13 pm
by hurstshifter
Lucas wrote:In the bullet::createbullets() function could you not just get the x, y of the player as you normally would?
For example, since you must have created an instance of the player somewhere, for the bullets x, y could you not just say something like:
Code: Select all
x = PlayerInstance.get_x();
y = PlayerInstance.get_y();
or now that I'm thinking about it a little more instead of hard coding the players instance into the class function like that you could just pass the function a pointer to your player instance.
That way you wouldn't be limited to only creating bullets at the player.
Code: Select all
void bullets::createbullets(player *PlayerInstance)
{
x = PlayerInstance->get_x();
y = PlayerInstance->get_y();
}
I hope that made sense, and I hope my code is ok, my thoughts are in the right place I'm just having trouble putting them into words :P
Hmm, I think I'm a bit confused when you refer to the playerInstance. I assume what you mean is the object of my class Player that I create when running the game. Although I could be way off here, Object Oriented programming is relatively new to me so I apologize. My main question here is really "Is it possible to pass use a function from another class within a seperate class?". I think I see where you are getting at with passing a pointer to the player object to the createbullets() function, but this is admittedly going over my head.
I also see that I could instead localize these functions into the player class, but that doesn't really help me learn in the long run.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 6:23 pm
by Falco Girgis
Dudes...
Code: Select all
void bullets::createbullets(Player &player)
{
SDL_Rect pTemp = player.getprect();
bulletRect.x = pTemp.x;
bulletRect.y = (pTemp.y - 10);
bDead = 1;
bVel = 1;
}
That will do what you want it to do, provided that
is a public function.
edit: I will elaborate. When you call bullets::createbullets() now, you should be passing the player object to it, like so:
Since your createbullets function receives the Player class with '&', it is a reference. Now you can refer to player as you normally would within this function and call its member.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 6:37 pm
by hurstshifter
GyroVorbis wrote:Dudes...
Code: Select all
void bullets::createbullets(Player &player)
{
SDL_Rect pTemp = player.getprect();
bulletRect.x = pTemp.x;
bulletRect.y = (pTemp.y - 10);
bDead = 1;
bVel = 1;
}
That will do what you want it to do, provided that
is a public function.
edit: I will elaborate. When you call bullets::createbullets() now, you should be passing the player object to it, like so:
Since your createbullets function receives the Player class with '&', it is a reference. Now you can refer to player as you normally would within this function and call its member.
That's what I thought too, but doing that now gives me a warning that an overloaded function of player::getprect(Bullets &bullets) does not exist. Adding Bullets &bullets into the formal parameters of that function in the header file doesn't solve the issue. Take a look at the current classes and the functions in question...
Code: Select all
class Player
{
private:
SDL_Rect p;
int pVel;
public:
Player();
SDL_Rect getprect();
void input();
void move();
void draw();
};
class Bullets
{
private:
SDL_Rect bulletRect;
int bVel;
int bDead; //has the bullet collided with an object or left the screen. 0 = dead
public:
Bullets();
void createbullets();
void updatebullets();
void checkbullets();
void killbullets();
};
SDL_Rect Player::getprect()
{
return p;
}
void Bullets::createbullets(Player &player)
{
SDL_Rect pTemp = player.getprect();
bulletRect.x = pTemp.x;
bulletRect.y = (pTemp.y - 10);
bDead = 1;
bVel = 1;
}
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 6:53 pm
by Bakkon
Code: Select all
public:
Bullets();
void createbullets(Player &player);
void updatebullets();
void checkbullets();
void killbullets();
You need that parameter in your prototype.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 7:02 pm
by hurstshifter
Bakkon wrote:Code: Select all
public:
Bullets();
void createbullets(Player &player);
void updatebullets();
void checkbullets();
void killbullets();
You need that parameter in your prototype.
I have it there. I had pasted in the code before I edited it. Despite how this looks like it should work, I'm still getting some errors...
classes.h(28) : error C2061: syntax error : identifier 'player'
classes.h(28) : error C2061: syntax error : identifier 'player'
classes.cpp(140) : error C2511: 'void bullets::createbullets(player &)' : overloaded member function not found in 'bullets'
classes.h(20) : see declaration of 'bullets'
Code: Select all
class bullets
{
private:
SDL_Rect bulletRect;
int bVel;
int bDead; //has the bullet collided with an object or left the screen. 0 = dead
public:
bullets();
void createbullets(player &playerRef);
void updatebullets();
void checkbullets();
void killbullets();
};
void bullets::createbullets(player &playerRef)
{
SDL_Rect pTemp = playerRef.getprect();
bulletRect.x = pTemp.x;
bulletRect.y = (pTemp.y - 10);
bDead = 1;
bVel = 1;
}
This one is really annoying. Thanks for everyone's help so far.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 7:21 pm
by teamtwentythree
Its been a while since I've done C++, but are your class names matching in case? It seems like you went from camel case to all lowercase.
Player != player in C++
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 7:23 pm
by hurstshifter
teamtwentythree wrote:Its been a while since I've done C++, but are your class names matching in case? It seems like you went from camel case to all lowercase.
Player != player in C++
Sorry, I manually typed some of the code in which is why it was capitalized. As a rule, I always begin functions and variables with lowercase letters so yes they are the same I just double checked.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 7:25 pm
by Bakkon
As t23 said, make sure the Player class is capatilized. Also, if you have seperate source files, make sure you're including player.h in the bullet header file.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 8:00 pm
by hurstshifter
Bakkon wrote:As t23 said, make sure the Player class is capatilized. Also, if you have seperate source files, make sure you're including player.h in the bullet header file.
They are capitalized. I have a class.h and class.cpp file for my classes. Maybe thats where my problem is. Should I be seperating each class into their own header/cpp files?
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 8:49 pm
by Innerscope
Why are we passing a player object to a bullet? That doesn't seem right to me...
Should I be seperating each class into their own header/cpp files?
Yes, this is usually recommended-> Player.h, Player.cpp, Bullet.h, Bullet.cpp. Where the header file contains all your member variables and function protocols, and the cpp file includes the header and has all the function implementations.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 8:51 pm
by hurstshifter
Innerscope wrote:Why are we passing a player object to a bullet? That doesn't seem right to me...
We are doing this so the bullet knows where to draw itself. We get the x and y offsets of the player and draw the bullet directly above it.
I'll try separating the classes into different source files. Not sure if this is my problem but hey, anything is worth a shot.
Re: Calling a class function from another class
Posted: Sat Jun 13, 2009 9:04 pm
by Bakkon
hurstshifter wrote:
I'll try separating the classes into different source files. Not sure if this is my problem but hey, anything is worth a shot.
It most likely won't be what's causing errors, but it helps organization and is good practice to prevent your code from becoming too overwhelming.
Innerscope wrote:
Why are we passing a player object to a bullet? That doesn't seem right to me...
I was think this too. I'd try letting the player class hold an array of bullet objects. Then create a fireBullet function for the player. Just grab the first bullet in the array that's currently bDead and shift its properties around. You'll have access to private player variables and then adjust the bullet with mutator functions in its own class.