C++ 错误:函数不可访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5844584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Function is inaccessible
提问by pighead10
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:
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:
武器.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"
错误与 getName() - “错误:函数 'Shopable::getName' 无法访问”
回答by
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
.
此外,名称 like_SHOPABLE_H_
在用户编写的 C++ 代码中是非法的,因为它们是为 C++ 实现保留的。忘记前导下划线并使用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.)
您可能需要重新考虑您的命名约定 - 通常,C++ 中的函数参数名称以小写字母开头。以大写字母开头的名称通常保留给用户定义的类型(即类、结构、枚举等)。
As a matter of interest, which C++ textbook are you learning from?
有趣的是,您是从哪本 C++ 教科书中学习的?
回答by Gustav Larsson
The inheritance must be public:
继承必须是公开的:
class Weapon : public Shopable
回答by jon-hanson
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
回答by Marc Mutz - mmutz
class
es default to private inheritance, struct
s to public. You're using class
, so you need to use : public Base
if you want to model "is-a":
class
es 默认为私有继承,struct
s 为公共继承。您正在使用class
,因此: public Base
如果您想对“is-a”建模,则需要使用:
class Weapon : public Shopable{ // added "public"
回答by Bo Persson
You get private inheritance by not specifying anything else. Try this instead
您可以通过不指定任何其他内容来获得私有继承。试试这个
class Weapon : public Shopable{