私有类型说明符是否会阻止对象“理解”对象?说明符?
我在获取函数接受枚举作为返回类型时遇到问题。在下面的代码中,有一个枚举:
Status{ DEAD, WOUNDED, HEALTHY }
和一个以 Status 作为返回类型的函数:
Status getStatus();
标头代码:
class Discovery
{
public:
void doCombat( int );
void setCombatThreshold( int );
void setHealth( int );
void setStatus( int );
Status getStatus();
private:
enum Status { DEAD, WOUNDED, HEALTHY };
Status charStatus;
int yourHealth;
int combatThreshold;
};
最初关联的函数定义如下:
Status Discovery::getStatus()
{
switch ( charStatus )
{
case DEAD:
return DEAD;
break;
case WOUNDED:
return WOUNDED;
break;
case HEALTHY:
return HEALTHY;
break;
};
}
我找到了这个答案: 从 C++ 基类中的函数返回枚举 这帮助我意识到我确实需要读取函数的第一行:
Discovery::Status Discovery::getStatus()
但是,我仍然接收我的标头代码出现“缺少类型说明符”错误。我意识到在私有访问说明符下声明“枚举状态”可能会产生影响,因此我将其移至标头代码中的公共访问说明符。成功了!但我想解释一下为什么它在私有访问说明符下不起作用。我在其他地方设法找到的是:
该类的对象无法访问私有数据成员。
我对发生的事情的解释是 - 使用私有访问说明符下的枚举类型定义,该函数(以及最终调用该函数的对象)不可能访问“理解”我的枚举类型,因此接受它作为返回类型。
但是 - 如果是这样的话,为什么我可以返回在私有访问说明符下声明的具有相同问题的变量?是因为它们的类型在其他地方已经被理解了,所以程序接受它们没有问题吗?
I was having a problem getting a function to accept an enum as a return-type. In the code below there's an enum:
Status{ DEAD, WOUNDED, HEALTHY }
and a function with Status as a return type:
Status getStatus();
Header Code:
class Discovery
{
public:
void doCombat( int );
void setCombatThreshold( int );
void setHealth( int );
void setStatus( int );
Status getStatus();
private:
enum Status { DEAD, WOUNDED, HEALTHY };
Status charStatus;
int yourHealth;
int combatThreshold;
};
Initially the associated function definition read:
Status Discovery::getStatus()
{
switch ( charStatus )
{
case DEAD:
return DEAD;
break;
case WOUNDED:
return WOUNDED;
break;
case HEALTHY:
return HEALTHY;
break;
};
}
I found this answer: returning enum from function in C++ base class which helped me realize I really needed the first line of the function to read:
Discovery::Status Discovery::getStatus()
But, I was still receiving a 'missing type specifier' error for my header code. I realized that having my 'enum Status' declaration under the private access-specifier might be making the difference, so I moved it to the public access-specifier in my header code. It worked! But I'd like some elucidation on why it wouldn't work under the private access-specifier. What I've managed to find elsewhere is:
Objects of the class cannot access private data members.
My interpretation of what happened being - with the enum-type definition under the private access-specifier, the function (and eventually an object calling that function) couldn't possibly access 'understand' my enum-type, and therefore accept it as a return type.
But - if that's the case, why am I allowed to return variables declared under the private access-specifier with the same problem? Is it because their types are already understood elsewhere, and so the program has no problem accepting them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
状态在使用后定义。将状态移到 get 方法之前。
如果您计划在类之外使用枚举,您将需要将枚举移至公共范围。
Status is defined after it's used. Move status up before the get method.
If you plan to use the enum outside of the class you will want to move the enum into public scope.
您需要更改声明的顺序。由于您在声明 Status 之前使用它,因此会出现错误。您必须将 Status 声明向上移动。
You need to change the order of the declarations. Since you're using Status prior to it being declared, it's giving you an error. You have to move the declaration of Status up.