[英]extern enum in header file
In my project, I have a header file containing the following lines.在我的项目中,我有一个 header 文件,其中包含以下几行。
extern enum class options {alpha, beta, gamma, theta};
extern options P1, P2;
Compilation gives me编译给了我
error: a storage class can only be specified for objects and functions
错误:只能为对象和函数指定存储 class
Why?为什么?
How can I rectify this?我该如何纠正这个问题?
Compiler: GCC 9.2.0编译器:GCC 9.2.0
extern
is applied to variables to specify that they have external linkage. extern
应用于变量以指定它们具有外部链接。 What you have is not a variable but an enum
definition, therefore extern
cannot be applied to it.您拥有的不是变量,而是
enum
定义,因此extern
不能应用于它。
What you can do is apply extern
to variables at file scope of the enum
type.您可以做的是将
extern
应用于enum
类型的文件 scope 中的变量。
For example, your header file would have:例如,您的 header 文件将具有:
enum options {alpha, beta, gamma, theta};
extern options myoption;
And in one source file you would have:在一个源文件中,您将拥有:
options myoption;
There's no storage associated with an enum class definition.没有与枚举 class 定义关联的存储。 So it doesn't make sense for it to be marked as extern.
因此,将其标记为 extern 是没有意义的。
A variable whose type is an enum class, on the other hand, can certainly be extern
because there's storage associated with a variable to hold the value of that variable.另一方面,类型为枚举 class 的变量当然可以是
extern
,因为存在与变量关联的存储来保存该变量的值。 The location of this storage is something that the linker must ultimately work out.这个存储的位置是 linker 最终必须解决的问题。
It makes sense to know of the existence of a variable without being responsible for designating storage for it at link time.知道变量的存在而不负责在链接时为其指定存储是有意义的。 This is how you get global variables.
这就是获取全局变量的方式。 For example:
例如:
main.cpp主文件
int foo = 5; // declaration and definition together.
other.cpp其他.cpp
extern int foo; // declaration only, (external definition still required elsewhere)
// but you can now use `foo` in this file. e.g.:
int examineFoo() {
return foo;
}
This is also useful if you want to provide a declaration of a global variable in a header file.如果您想在 header 文件中提供全局变量的声明,这也很有用。 For example:
例如:
main.cpp主文件
#include "foo.h"
int foo = 5;
foo.h foo.h
extern int foo;
other.cpp其他.cpp
#include "foo.h"
int examineFoo() {
return foo;
}
It's okay to encounter both the extern declaration and the definition in the same file.可以在同一个文件中同时遇到 extern 声明和定义。 eg:
例如:
extern int foo;
int foo = 5;
To fix exactly the example you provided, you can remove extern
from the enum class definition, where it does not make sense to have it.要准确修复您提供的示例,您可以从枚举 class 定义中删除
extern
,在该定义中使用它没有意义。
enum class options {alpha, beta, gamma, theta};
extern options P1, P2;
Remember that some non-extern definition of those variables P1
and P2
will be required in another module in order for your program to link if you make use of them.请记住,在另一个模块中需要对这些变量
P1
和P2
进行一些非外部定义,以便您的程序在使用它们时进行链接。
For you, you probably want the class to be defined in a header, along with an extern declaration of any global variables (yuck) you want in your program.对您而言,您可能希望在 header 中定义 class,以及您在程序中想要的任何全局变量 (yuck) 的外部声明。
options.h选项.h
enum class options {alpha, beta, gamma, theta};
extern options P1, P2;
options.cpp选项.cpp
#include "options.h"
options P1 = options::alpha, P2 = options::beta;
In options.cpp you include options.h to get the definition of the enum class.在 options.cpp 中包含 options.h 以获取枚举 class 的定义。 It also came with an extern definition of the global variables
P1
and P2
, which wasn't strictly necessary as they are going to be defined in this file anyway, but it's not illegal to do it.它还附带了全局变量
P1
和P2
的外部定义,这并不是绝对必要的,因为无论如何它们都将在此文件中定义,但这样做并不违法。
other.cpp其他.cpp
#include "options.h"
options examineOptionP1() {
return P1;
}
other.cpp is aware of the class definition for options
and is aware of extern declarations of P1 and P2. other.cpp 知道
options
的 class 定义,并且知道 P1 和 P2 的外部声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.