从C ++基类中的函数返回枚举

编程入门 行业动态 更新时间:2024-10-28 21:21:12
本文介绍了从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文件中我有这个

in the .cpp file I have this

HANDLER_PRIORITY Handler::GetPrioity() { return PRIORITY_0; }

我收到一个编译错误,缺少类型说明符 - int假设注意: C ++不支持default-int

I get a compilation error, "missing type specifier - int assumed. Note: C++ does not support default-int"

我知道不连接C,C ++不支持default-int返回。但是为什么它不能识别枚举返回类型。如果我使用int / void替换HANDLER_PRIORITY的返回类型,或者如果我在类本身中定义方法,它可以正常工作。 (内联)或将返回类型更改为Handler :: HANDLER_PRIORITY。

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.

我在VS 2008上。

I am on VS 2008.

推荐答案

您需要

Handler::HANDLER_PRIORITY Handler::GetPriority() { ... }

编辑:抱歉没看到你的帖子的其余部分至于为什么会这样, HANDLER_PRIORTY 没有全局作用域。它是 Handler 的成员,不低于任何其他。所以当然你必须告诉编译器它在哪里,即使用 Handler :: 。

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 of Handler no less than any other. So of course you have to tell the compiler where it is, i.e. use Handler::.

更多推荐

从C ++基类中的函数返回枚举

本文发布于:2023-11-15 22:03:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   类中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!