pighead10
pighead10

Reputation: 4305

Class inheritance: Function is inaccessible

I'm getting this error, but I thought I would only get it if the member's protection level was too high and made it inaccessible, but I'm getting it 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 a random function with the correct includes:

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

The error is with getName() - "Error: function 'Shopable::getName' is inaccessible"

Upvotes: 37

Views: 118655

Answers (5)

jon hanson
jon hanson

Reputation: 9408

You're using private inheritance:

class Weapon : Shopable

So the fact that a Weapon is a Shopable is not visible to other classes. Change it to public inheritance:

class Weapon : public Shopable

Upvotes: 13

user2100815
user2100815

Reputation:

You want public inheritance:

 class Weapon : Shopable

should be:

 class Weapon : public Shopable

Also, names like _SHOPABLE_H_ are illegal in user written C++ code, as they are reserved for the C++ implementation. Forget the leading underscores and 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 the unnecessary overhead of copying the string.

You might want to rethink your naming convention - typically, function parameter names in C++ begin with a lower case latter. Names beginning with uppercase letters are typically reserved for user-defined types (i.e. classes, struct, enums etc.)

As a matter of interest, which C++ textbook are you learning from?

Upvotes: 84

Marc Mutz - mmutz
Marc Mutz - mmutz

Reputation: 25293

classes default to private inheritance, structs to public. You're using class, so you need to use : public Base if you want to model "is-a":

class Weapon : public Shopable{ // added "public"

Upvotes: 7

Bo Persson
Bo Persson

Reputation: 92261

You get private inheritance by not specifying anything else. Try this instead

class Weapon : public Shopable{

Upvotes: 5

Gustav Larsson
Gustav Larsson

Reputation: 8487

The inheritance must be public:

class Weapon : public Shopable

Upvotes: 13

Related Questions