I have Hedgehog
class, that inherits Body2D
class. I want Hedgehog
to use default Body2D
drawing function (it's render(SDL_Surface* )
) because it hasn't any class-specific behaviour in this case, but I've got an error:
||=== Build: Debug in SDLApples (compiler: GNU GCC Compiler) ===|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp||In function ‘int main(int, char**)’:|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|84|error: ‘void Body2D::render(SDL_Surface*)’ is inaccessible within this context|
engine/include/Body2D.h|37|note: declared here|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|84|error: ‘Body2D’ is not an accessible base of ‘Hedgehog’|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|85|error: ‘void Body2D::render(SDL_Surface*)’ is inaccessible within this context|
engine/include/Body2D.h|37|note: declared here|
/home/maggistrator/Development/cpplabs/labs/Hetchehog and Apples/SDLApples/src/main.cpp|85|error: ‘Body2D’ is not an accessible base of ‘Apple’|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
My Hedgehog
class is just a simple body that moves from one side of screen to another and back again. Here is it's source code. Body2D
class is abstract body in 2D world that can be drawn, updated and can interact in other bodies in the same World
. Here is Body2D
's header file and source. And here is main
that calls them:
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include "Hedgehog.cpp"
#include "Apple.cpp"
#include "World.h"
#include <vector>
#define APPLE_COUNT 5
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
//------------------------------------------------------------------
//OBJECTS HERE
World scene;
Hedgehog hg(440, 280, scene);
std::vector<Apple> apples;
for(int i = 0; i < APPLE_COUNT; i++)
{
apples.push_back(Apple(i*100, -50, scene));
}
//------------------------------------------------------------------
// program main loop
bool done = false;
while (!done)
{
//======message processing loop======
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
if (event.key.keysym.sym == SDLK_ESCAPE) done = true;
if (event.key.keysym.sym == SDLK_RETURN) ; //Apples fall here
break;
}
} // end switch
} //======end of message processing=====
// UPDATE
hg.update();
for(Apple apple: apples) apple.update();
// RENDER
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
hg.render(screen);
for(Apple apple: apples) apple.render(screen);
SDL_Flip(screen);
} // end main loop
printf("Exited cleanly\n");
return 0;
}
So the question is: can I use base class's function without overriding it? Or default behaviour is allowed only for objects of base class itself?
p.s. I was learning Java for several years, but now I have to use C++, so may be I'm making some common Java-programmer's mistake in understanding language vision of inheritance?