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.
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.
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.
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.
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.
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.
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.
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...
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.
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.
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?
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.
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.