[Solved] Calling a class function from another class

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

[Solved] Calling a class function from another class

Post 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.
Last edited by hurstshifter on Sat Jun 13, 2009 10:17 pm, edited 1 time in total.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Lucas
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Sun May 24, 2009 11:28 am
Favorite Gaming Platforms: PC, SNES, Dreamcast
Programming Language of Choice: C++
Location: UK

Re: Calling a class function from another class

Post 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
I don't see any invisible walls...
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Calling a class function from another class

Post 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.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Calling a class function from another class

Post 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

Code: Select all

Player::getprect()
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:

Code: Select all

bullsets.createbullets(player)
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.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post 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

Code: Select all

Player::getprect()
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:

Code: Select all

bullsets.createbullets(player)
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;
}

"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Calling a class function from another class

Post by Bakkon »

Code: Select all

public:
   Bullets();
   void createbullets(Player &player);
   void updatebullets();
   void checkbullets();
   void killbullets();
You need that parameter in your prototype.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
teamtwentythree
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 72
Joined: Wed Apr 16, 2008 12:19 am
Location: Seattle, WA

Re: Calling a class function from another class

Post 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++
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Calling a class function from another class

Post 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.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post 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?
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Calling a class function from another class

Post 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.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Calling a class function from another class

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Calling a class function from another class

Post 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.
Post Reply