-1

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?

OldTeaOwl
  • 245
  • 5
  • 13
  • 1
    One quick thing to be careful of is that if you just have a variable, by itself, such as `Apple apple`, then that variable has its own copy of its data. It is _not_ a reference into `apples`. Just make sure to use `Apple const& apple` or `Apple & apple` (depending on whether you intend to keep `apple` constant or modify it). Or `auto&& apple` if you're feeling lazy. – hegel5000 Apr 04 '20 at 00:10
  • Does this answer your question? [Default class inheritance access](https://stackoverflow.com/questions/3811424/default-class-inheritance-access) – vasek Apr 04 '20 at 00:16
  • For the future: if the compiler gives you an error I can guarantee you that somebody else has had the same problem. Google it! – Kevin Apr 04 '20 at 00:42
  • @hegel5000 thanks, it's important notice, I'll keep it in mind! – OldTeaOwl Apr 05 '20 at 00:37
  • @vasek straight answer without comparsions with structures is clearer, but it's useful as expanded explanation, so thanks for it ;) – OldTeaOwl Apr 05 '20 at 00:45

1 Answers1

1

In C++ you have to use access modifier when inheriting from base class, otherwise private inheritance is assumed by default for classes, see this answer. So your code should be fixed by inheriting using public modifier:

class Hedgehog : public Body2D

There is no similar principle in Java, extends keyword can not change access modifiers.

vasek
  • 2,759
  • 1
  • 26
  • 30