访问命名空间中的枚举
在标头中,我有这样的设置
namespace NS {
typedef enum { GOOD, BAD, UGLY }enum_thing;
class Thing {
void thing(enum_thing elem);
}
}
,当然还有与该标头一起使用的另一个 cpp 文件。然后我有一个包含 main() 的线程 cpp 文件。在此 cpp 文件中,我使用该枚举传递给方法 thing()。
using namespace NS;
int main() {
Thing t();
t.thing(BAD);
}
当然,我从 G++ 中收到其他错误,指出未声明 BAD。对我如何克服这个错误有任何帮助吗?
In a header I have a setup like this
namespace NS {
typedef enum { GOOD, BAD, UGLY }enum_thing;
class Thing {
void thing(enum_thing elem);
}
}
and of course another cpp file that goes along with that header. Then I have a thread cpp file that contains main(). In this cpp file I use that enum to pass to the method thing().
using namespace NS;
int main() {
Thing t();
t.thing(BAD);
}
and of course I get other errors from G++ saying BAD was not declared. Any help on how I could overcome this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在纠正了示例代码中的许多小语法错误后,它对我来说编译得很好。检查您的姓名拼写是否正确。你能以 NS::BAD 的形式访问枚举吗?也许您没有包含正确的标头?确保你有
#include "FileWithEnum.h"
在顶部。自己测试一下:
http://codepad.org/Uw0XjOlF
After correcting numerous little syntax errors in the sample code, it compiles just fine for me. Check that you've spelled the names correctly. Can you access the enum as NS::BAD? Perhaps you haven't included the correct header? Make sure you have
#include "FileWithEnum.h"
at the top.Test it yourself:
http://codepad.org/Uw0XjOlF
你能避免使用 typedef 吗?只要这样做:
Can you avoid using a typedef? Just do:
它应该有效。它对我有用(Mystagogue 的变体也应该有效)。据我所知您还有其他错误消息?
您可能只需要修复标头以使其语法正确,例如在类 Thing 的末尾添加分号等。当标头正常时,有关 BAD not in namespace 的消息也应该消失。
It should work. It does for me (the variant by Mystagogue should also work). I understand you had some other error messages ?
You probably just have to fix the header to be syntaxically correct, like putting a semi-colon at the end of class Thing, etc. When the header will be OK, the message about BAD not in namespace should also disappear.