c++ - Error: Function is inaccessible -


i'm getting error, thought if member's protection level high , made inaccessible, i'm getting anyway.

shopable.h:

#ifndef _shopable_h_ #define _shopable_h_  #include "library.h"  class shopable{ private:     std::string name;     int cost;     std::string description; public:     std::string getname() const{return name;}     int getcost() const {return cost;}     virtual std::string getdesc() const = 0; };  #endif 

weapon.h:

#ifndef _weapon_h_ #define _weapon_h_  #include "globals.h" #include "shopable.h"  class weapon : shopable{ private:     int damage; public:     weapon(int cost,int damage,std::string name) : cost(cost), damage(damage), name(name){}     std::string getdesc() const{         return getname()+"\t"+tostring(damage)+"\t"+tostring(cost);     }     int damage(entity *target){         int damagedealt = 0;         //do damage algorithm things here         special();         return damagedealt;     } };  #endif 

some line in random function correct includes:

std::map< std::string, weapon* > weapons; weapon* none = new weapon(0,0,"none"); weapons[none->getname()] = none; 

the error getname() - "error: function 'shopable::getname' inaccessible"

you want public inheritance:

 class weapon : shopable 

should be:

 class weapon : public shopable 

also, names _shopable_h_ illegal in user written c++ code, reserved c++ implementation. forget leading underscores , use shopable_h.

and:

 weapon(int cost,int damage,std::string name) 

should be:

 weapon(int cost,int damage, const std::string & name ) 

to avoid unnecessary overhead of copying string.

you might want rethink naming convention - typically, function parameter names in c++ begin lower case latter. names beginning uppercase letters typically reserved user-defined types (i.e. classes, struct, enums etc.)

as matter of interest, c++ textbook learning from?


Comments