简体   繁体   English

不确定如何在头文件中实现可访问的枚举?

[英]Not sure how to implement accessible enum in header file?

In a header file I have something basically like this 在头文件中,我基本上是这样的

#ifndef blah
#define blah

enum DataType
{
    TextData,
    IntData

};


#endif

And in another file I have 在另一个文件中

#include "previousheaderdefinedabove"

int main(int argc, char *argv[])
{
    std::cout << DataType::TextData;


    return 1;
}

However, when I try to compile I get 但是,当我尝试编译时,我得到

main.cpp:13:18: error: expected a class or namespace
    std::cout << DataType::TextData;

Not sure what I'm doing wrong, any help is much appreciated! 不知道我在做什么错,非常感谢您的帮助! I've seen enums being used as classes but is there a reason why the namespcae isn't enough? 我已经看到枚举被用作类,但是为什么namepcae不够是有原因的吗?

The enum is neither a namespace nor a class; 枚举既不是名称空间,也不是类。 its values are in the same scope as the definition. 其值与定义在同一范围内。 In the above example, the enum values are in the global namespace. 在上面的示例中,枚举值位于全局名称空间中。

That means, in main, you should have 这意味着,基本上,您应该

std::cout << TextData;

instead of 代替

std::cout << DataType::TextData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM