C++ 错误:函数不可访问

C++ 错误:函数不可访问,c++,inheritance,protection,C++,Inheritance,Protection,我得到了这个错误,但我想我只会得到它,如果成员的保护级别太高,使其无法访问,但我得到它无论如何 可购买。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{

我得到了这个错误,但我想我只会得到它,如果成员的保护级别太高,使其无法访问,但我得到它无论如何

可购买。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
武器.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
随机函数中具有正确值的某些行包括:

std::map< std::string, Weapon* > weapons;
Weapon* none = new Weapon(0,0,"None");
weapons[none->getName()] = none;
std::map武器;
武器*无=新武器(0,0,“无”);
武器[none->getName()]=none;

getName()出现错误-“错误:函数'Shopable::getName'不可访问”

继承必须是公共的:

class Weapon : public Shopable

您正在使用私有继承:

class Weapon : Shopable
class Weapon : public Shopable
 class Weapon : Shopable
因此,武器是可购买的这一事实对于其他职业来说是不可见的。将其更改为公共继承:

class Weapon : Shopable
class Weapon : public Shopable
 class Weapon : Shopable

您可以通过不指定任何其他内容来获得私有继承。试试这个

class Weapon : public Shopable{

class
es默认为私有继承,
struct
s默认为公共继承。您使用的是
,因此,如果要对“is-a”建模,则需要使用
:public Base


您想要公共继承:

class Weapon : Shopable
class Weapon : public Shopable
 class Weapon : Shopable
应该是:

 class Weapon : public Shopable
 Weapon(int Cost,int Damage, const std::string & Name )

也可以使用C++编写代码,比如“C++”、“SubabeLeYH/<代码”之类的名称在C++实现中是非法的。忘记前导下划线,使用

shoppable\u H

以及:

应该是:

 class Weapon : public Shopable
 Weapon(int Cost,int Damage, const std::string & Name )
以避免复制字符串的不必要开销

您可能想重新思考命名约定——通常,C++中的函数参数名称从后面的小写开始。以大写字母开头的名称通常保留给用户定义的类型(即类、结构、枚举等)


作为一件趣事,你从C++学习的教科书中学习了什么?

不是隐式继承<代码>保护< /代码>?@古斯塔夫:不,它是公开的<代码>结构> /代码>,私下用于<代码>类< /代码>。@迈克我必须检查,因为我好奇,你是对的。我没有用教科书,只是从互联网上的一些地方收集点点滴滴。