快速问题快速回答 [英] Quick Question Quick Answer

查看:100
本文介绍了快速问题快速回答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采取以下措施:

enum Sense

{

愿景,

听证会,

触摸,

气味

};

无效捷豹(const Sense& sense)

{

返回;

}

int main(无效)

{

Sense感觉=听力;


美洲虎(感觉);


美洲虎(听力);


听证会;


Jaguar(Sense :: Hearing); //错误`Sense''不是聚合类型


返回0;

}


怎么做我告诉它我指的是听力。来自enumSense,和

不是名为Hearing的本地变量?!

-JKop

Take the following:
enum Sense
{
Vision,
Hearing,
Touch,
Smell
};
void Jaguar(const Sense& sense)
{
return;
}
int main(void)
{
Sense sense = Hearing;

Jaguar(sense);

Jaguar(Hearing);

int Hearing;

Jaguar(Sense::Hearing); //ERROR `Sense'' is not an aggregate type

return 0;
}

How do I tell it that I''m referring to "Hearing" from the enum "Sense", and
not the local variable entitled "Hearing"?!
-JKop

推荐答案

>我如何告诉它我指的是听力。来自enum

" Sense"和
> How do I tell it that I''m referring to "Hearing" from the enum
"Sense", and
不是名为Hearing的本地变量?!
not the local variable entitled "Hearing"?!




我认为你只是在C ++中遇到枚举类型的基本限制。


枚举本身就是整数值,正如你所指出的那样,没有

实现这种区分的真正方法。


一种可能的解决方案是将枚举包含在命名空间中。


例如。


命名空间感觉{

enum SenseType

{

视力,听力,触觉,气味

};

}


void Jaguar(const Sense :: SenseType& sense);


现在你的功能可以使用


Jaguar(Sense :: Hearing);

JLR



I think you just hit a fundamental limitation of enum types in C++.

enums are inherently integer values, and as you point out, there is no
real way to make this distinction.

One possible solution is to enclose your enum in a namespace.

Eg.

namespace Sense{
enum SenseType
{
Vision, Hearing, Touch, Smell
};
}

void Jaguar(const Sense::SenseType& sense);

now you function can be called using

Jaguar(Sense::Hearing);
JLR


JKop写道:
JKop wrote:
Jaguar(Sense :: Hearing); //错误`Sense''不是聚合类型
Jaguar(Sense::Hearing); //ERROR `Sense'' is not an aggregate type




试试::听证



Try ::Hearing


JKop写道:
我怎么告诉它我指的是听力?来自enumSense,并且
不是标题为Hearing的本地变量?!
How do I tell it that I''m referring to "Hearing" from the enum "Sense", and
not the local variable entitled "Hearing"?!




不要出于同样的原因这样做你不这样做:


int blah()

{

浮动a;

int a;


...

}



Don''t do this for the same reason you don''t do this:

int blah()
{
float a;
int a;

...
}


这篇关于快速问题快速回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆