C++ 无法访问类“士兵”中声明的私有成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17720279/
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
cannot access private member declared in class 'Soldier'
提问by user2594877
I'm new to OOP and I do not understand how to pass arguments to classes.
我是 OOP 的新手,我不明白如何将参数传递给类。
The class' declaration:
班级声明:
class Soldier
{
Soldier(int SetHealth, int SetStrength);
private:
int health;
int strength;
public:
void attacked();
void healed();
int getHealth();
int getStrength();
};
Definition of the constructor:
构造函数的定义:
Soldier::Soldier(int SetHealth, int SetStrength):
health(SetHealth),
strength(SetStrength)
{
}
When I try passing arguments to the class it says this:
当我尝试将参数传递给类时,它说:
1>------ Build started: Project: ConsoleApplication6, Configuration: Debug Win32 ------
1> ConsoleApplication6.cpp
1>c:\users\user\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(11): error C2248: 'Soldier::Soldier' : cannot access private member declared in class 'Soldier'
1> c:\users\user\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\soldier.h(7) : see declaration of 'Soldier::Soldier'
1> c:\users\user\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\soldier.h(6) : see declaration of 'Soldier'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
采纳答案by doctorlove
People have already answered this now, but explicitly:
人们现在已经回答了这个问题,但明确地:
class Soldier
{
private:
int health;
int strength;
public:
Soldier(int SetHealth, int SetStrength);
void attacked();
void healed();
int getHealth();
int getStrength();
};
BTW What are attached and healed going to do? They take no parameters and return nothing. Odd.
顺便说一句,附着和治愈要做什么?它们不接受任何参数,也不返回任何内容。奇怪的。
Edit (in light of recent down vote):
编辑(根据最近的否决票):
Moving the constructor to the public "section", makes it public
. Things start off as private
until you say otherwise.
将构造函数移动到公共“部分”,使其public
. 事情开始,private
直到你另有说法。
回答by undu
Your constructor is private
(just as the error message says).
By default, every member of a Class
is private
if you don't specify an access level.
您的构造函数是private
(就像错误消息所说的那样)。默认情况下,如果您不指定访问级别,则 a 的每个成员Class
都是private
。
回答by Keval Doshi
The constructor that you have defined has a access level of
您定义的构造函数的访问级别为
private
By default if one does not mention anything, the access level is private So change it to public for access.
默认情况下,如果没有提及任何内容,则访问级别是私有的,因此将其更改为公开以进行访问。
回答by Luchian Grigore
A class's members are implicitly private
unless you change the access level to public
or protected
, so Soldier::Soldier(int SetHealth, int SetStrength);
is private - ergo you can't access it.
private
除非您将访问级别更改为public
或protected
,否则类的成员是隐式的,因此Soldier::Soldier(int SetHealth, int SetStrength);
是私有的 -因此您无法访问它。
回答by Петър Петров
changet constructor declaration to this public Soldier(int SetHealth, int SetStrength);
将构造函数声明更改为此 public Soldier(int SetHealth, int SetStrength);