简体   繁体   English

C ++外部枚举

[英]C++ extern enums

i found already this: extern enum in c++ But it didn't helped me much. 我已经发现了这个: c ++中的extern enum但它对我帮助不大。 I have a config.h and a config.cpp in the config.hi have my enum: 我有一个config.h和config.hi中的config.cpp有我的枚举:

#ifndef CONFIG_H
#define CONFIG_H
extern enum Items;
#endif

And in my config.cpp i have the enum declared: 在我的config.cpp中,我声明了枚举:

#include "config.h"
 enum Items {
    PAC = 'C', GHOST = '@', FRUIT = 'o', POINTS = '.', WALL = '#', EMPTY = ' ', UNDEFINED = '+'
} fieldItems;

But if i try to compile it the compiler gives me the following Errors: 但是如果我尝试编译它,编译器会给我以下错误:

config.cpp:20:13: error: a storage class can only be specified for objects and functions
 extern enum Items;
         ^

You won't be able to define an enum externally. 您将无法在外部定义enum In C++11 you can forward declare an enum , however: 在C ++ 11中,您可以转发声明enum ,但是:

enum class Items: char;

... and later define it: ......然后定义它:

enum class Items: char {
       PAC = 'C', GHOST = '@', FRUIT = 'o', POINTS = '.', WALL = '#', EMPTY = ' ', UNDEFINED = '+'
};

Note, however, that you can use the enumerator tags only where the definition was seen. 但请注意,只能在看到定义的位置使用枚举器标记。

Based on your comment about multiple definitions, you don't have include guards in your header and the same header gets included multiple times. 根据您对多个定义的评论,您的标头中没有包含保护,并且多次包含相同的标头。 That is, seems you could have a header something like this: 也就是说,似乎你可以有这样的标题:

// items.h
#ifndef INCLUDED_ITEMS
#define INCLUDED_ITEMS

enum Items { ... };

#endif

From the looks of it, you are trying to fold the representation of the game into into the definition of their names. 从它的外观来看,你试图将游戏的表现形式折叠成他们名字的定义。 That probably doesn't work too well, especially as they may change appearance (you might want to have PAC alternate between C and O , for example). 这可能效果不好,特别是因为它们可能会改变外观(例如,您可能希望PACCO之间交替)。 Most likely the actual code will talk about these names and you'll have the in multiple places. 很可能实际的代码会谈论这些名称,你会在多个地方。 Just use an enum to define the different items not defining any value for them and define their representation elsewhere: 只需使用enum来定义不为它们定义任何值的不同项,并在其他地方定义它们的表示:

enum Items { PAC, GHOST, FRUIT, POINTS, WALL, EMPTY, UNDEFINED };
extern char ItemDisplay[];

// elsewhere

char ItemDisplay[] = { 'C', '@', 'o', '.', '#', ' ', '+' };

... and then just use ItemDisplay[item] whenever you need to draw an item . ...然后只需在需要绘制item时使用ItemDisplay[item]

The answer to your question is that you need to use this to extern extern Items fieldItems; 你的问题的答案是你需要使用它来extern extern Items fieldItems; try to have the enum definition in the header file and then try the extern 尝试在头文件中使用枚举定义,然后尝试extern

You defined a type, not a variable. 您定义了一个类型,而不是一个变量。 A type cannot be static or extern. 类型不能是静态的或外部的。 You must declare a variable of that type. 您必须声明该类型的变量。

extern enum Items doesn't declare anything: it just tells you that there is an enum type called Items. extern enum Items没有声明任何内容:它只是告诉你有一个名为Items的枚举类型。 I think what you really want is 我想你真正想要的是

extern enum Items fieldItems;

Alternatively 另外

enum Items;
extern Items fieldItems;

extern enum Items; is a syntax error. 是语法错误。

You use extern to declare a link-time entity you want to refer to that is defined elsewhere. 您可以使用extern声明要在其他位置定义的要引用的链接时实体。 However, enumerations are collections of compile-time constants; 但是,枚举是编译时常量的集合; they do not have a representation in object files. 它们在目标文件中没有表示。

The ideology of C and C++ is such that every compilation unit must be self-sufficient for the purpose of compilation, that is, all necessary compile-time entities must be defined within the compilation unit. C和C ++的意识形态是这样的,每个编译单元必须是自足的,以便进行编译,也就是说,必须在编译单元中定义所有必需的编译时实体。

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

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