问题描述
采取以下措施:
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
推荐答案
我认为你只是在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
试试::听证
Try ::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;
...
}
这篇关于快速问题快速回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!