从 C++ 中的函数返回枚举基类
我遇到了以下代码,
class Handler
{
public:
Handler() {}
~Handler() {}
enum HANDLER_PRIORITY {PRIORITY_0, PRIORITY_1, PRIORITY_2};
virtual HANDLER_PRIORITY GetPriority();
private:
HANDLER_PRIORITY m_priority;
}
在 .cpp 文件中我遇到了
HANDLER_PRIORITY Handler::GetPrioity()
{
return PRIORITY_0;
}
编译错误,“缺少类型说明符 - int 假定。注意:C++ 不支持默认 int”
我知道取消链接 C、C++ 不支持默认-int 返回。但为什么它不能识别枚举返回类型。如果我用 int/ void 替换 HANDLER_PRIORITY 的返回类型,或者如果我在类本身中定义该方法,它就可以正常工作。 (内联)或将返回类型更改为 Handler::HANDLER_PRIORITY。
我用的是VS 2008。
I came across the following code,
class Handler
{
public:
Handler() {}
~Handler() {}
enum HANDLER_PRIORITY {PRIORITY_0, PRIORITY_1, PRIORITY_2};
virtual HANDLER_PRIORITY GetPriority();
private:
HANDLER_PRIORITY m_priority;
}
in the .cpp file I have this
HANDLER_PRIORITY Handler::GetPrioity()
{
return PRIORITY_0;
}
I get a compilation error, "missing type specifier - int assumed. Note: C++ does not support default-int"
I know that unlinke C, C++ does not support default-int return. but why would it not recognize an enum return type. It works fine if I replace return type from HANDLER_PRIORITY with int/ void, OR if I define the method in the class itself. (inline) OR change the return type to Handler::HANDLER_PRIORITY.
I am on VS 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
编辑:抱歉没有看到您帖子的其余部分。至于为什么会出现这种情况,HANDLER_PRIORTY 没有全局作用域。它是
Handler
的成员,与其他成员一样。所以当然你必须告诉编译器它在哪里,即使用Handler::
。You need
Edit: Sorry didn't see the rest of your post. As for why this is the case,
HANDLER_PRIORTY
doesn't have global scope. It's a member ofHandler
no less than any other. So of course you have to tell the compiler where it is, i.e. useHandler::
.