I am writing a text game. Transferred common functions to another class and an error occurred. How to fix? Here is a mistake.

main.cpp: In function 'int main(int, char**)': main.cpp:13:47: error: 'void Living::set_pos(Map*)' is inaccessible within this context player.set_pos ( map->find_map_coord ( 'D' ) ); ^ In file included from Player.h:3, from main.cpp:4: Living.h:11:8: note: declared here void set_pos ( Map *pos ); ^~~~~~~ main.cpp:13:47: error: 'Living' is not an accessible base of 'Player' player.set_pos ( map->find_map_coord ( 'D' ) ); ^ make: *** [makefile:2: all] Error 1 

Here is main.cpp

 #include <iostream> #include <cstdio> #include "Loader.h" #include "Player.h" int main(int argc, char **argv) { Loader loader; loader.load ( "map" ); Map *map = loader.get_map ( ); map->set_sizes ( loader.width, loader.height ); Player player; player.set_pos ( map->find_map_coord ( 'D' ) ); return 0; } 

Here is living.h

 #ifndef __living__ #define __living__ #include "Map.h" #include <cstring> class Living { protected: Map *pos; char *name; public: void set_pos ( Map *pos ); void set_name ( const char *name ); void walk_north ( ); void walk_west ( ); void walk_south ( ); void walk_east ( ); }; #endif 

Here is living.cpp

 #include "Living.h" void Living::set_name ( const char *name ) { int length = strlen ( name ); this->name = new char [ length + 1 ]; strncpy ( this->name, name, length ); this->name [ length ] = 0; } void Living::walk_north ( ) { if ( pos->north->get_place ( ) == '*' ) { } pos = pos->north; } void Living::walk_west ( ) { } void Living::walk_south ( ) { } void Living::walk_east ( ) { } void Living::set_pos ( Map *pos ) { this->pos = pos; } 

Here is player.h

 #include <string.h> #include "Map.h" #include "Living.h" class Player : Living { }; 

    1 answer 1

    class parents are private by default. Naturally, you cannot access the private methods of the parent.

    You need this:

     class Player : public Living {}; 
    • Thanks, but I did it first, but there were other mistakes, and I didn’t know exactly why to make a public class, but now I understand. - xverizex