Answer» Hi. I'm currently in the process of writing a space shooter type-of-game in C++ using the SDL API. I have everything working great, but I'm having some trouble getting my spaceship to fire a laser. I know how to do this, but I'm using three different CLASSES that all inherit from each other, and I can't get anything to work from my Laser class. This is the error message I get:
In file included from space.cpp:11: classDefinitions.h: In member function 'void SpaceShip::handle_ship(bool&, SDL_Surface*, SDL_Surface*)': classDefinitions.h:85: error: 'handle_laser' was not declared in this scope
I have all three files as attachments in TXT format so I could send them. Thanks all for all of the help
[recovering disk space - old attachment deleted by admin]Generally, inheritance implies an is-a relationship between class types.
Code: [Select]class SpaceShip : public Alien A SpaceShip is-a Alien seems a bit weird;Same with the Laser... that doesn't help much though, heh.
I'm guessing that this is where you are getting the error:
Code: [Select] if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_SPACE) { handle_laser(scr, bGround)
Consider the message. "Not declared in scope". "But it's right above it!" you say; true, but look at the signatures:
SpaceShip::HandleShip and Laser::HandleLaser; HandleShip is within the SpaceShip class; HandleLaser is within the Laser Class.
Within the SpaceShip class, there is no "laser" instance; I think what may be confusing things here is that you are mixing up the implementations of the various classes. (that is, you write the implementations for the various routines in the same file) This of course isn't illegal, it's just not very common. Generally, the approach is to have a separate header file and a separate cpp file for each class definition; then in each other class header that uses them, you add a #include "classname.h".
I don't know if I explained that very well. In any case, within the implementations of a class, there is an UNSEEN argument- the "this" pointer, which points to the instance of the object the method "BELONGS" to; this allows methods to modify the class-instance data (for example, if you were to have two lasers on the game field, they would still use the same code for handling their actions, but the "this" pointer for each would point to something else. Of course, you aren't using any instance fields (such as positional data) which is a tad curious IMO but hey, WHATEVER works.
Long story short: you haven't created any instances of the Laser class; so you cannot call the instance methods of the Laser class directly from the Ship.
|