C++ 在不同的 .cpp 文件中使用 struct
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6882637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using struct in different .cpp file
提问by paras
I have two .cpp files in one project, main.cpp and myfile.cpp
我在一个项目中有两个 .cpp 文件,main.cpp 和 myfile.cpp
I have globaly defined struct mystruct in main.cpp, now I want to use this struct in myfile.cpp. When I write mystruct in a header file and include in both cpp files I get an error, saying mystruct redefinition. How should I solve this problem.
我在 main.cpp 中全局定义了 struct mystruct,现在我想在 myfile.cpp 中使用这个结构。当我在头文件中写入 mystruct 并包含在两个 cpp 文件中时,出现错误,说 mystruct redefinition。我应该如何解决这个问题。
回答by Martin Gunia
If you are trying to share the definitionof a struct among several compilation units (cpp files), the common way is this: Place the definition of your struct in a header file (mystruct.h). If the struct contains any methods (i.e. it is rather a class with all member public by default), you can implement them in mystruct.CPP file, or, if they're lightweight, directly within the struct (which makes them inlineby default).
如果您试图在多个编译单元(cpp 文件)之间共享一个结构体的定义,常用的方法是:将结构体的定义放在一个头文件(mystruct.h)中。如果结构体包含任何方法(即默认情况下它是一个所有成员为 public 的类),您可以在 mystruct.CPP 文件中实现它们,或者,如果它们是轻量级的,则直接在结构体中实现(这使得它们默认内联)。
mystruct.h:
mystruct.h:
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
struct MyStruct
{
int x;
void f1() { /* Implementation here. */ }
void f2(); /* Implemented in mystruct.cpp */
};
#endif
mystruct.cpp
mystruct.cpp
#include "mystruct.h"
// Implementation of f2() goes here
void MyStruct::f2() { ... }
You can use your struct in as many cpp files as you like, simply #include
mystruct.h:
您可以在#include
任意数量的cpp 文件中使用您的结构,只需mystruct.h:
main.cpp
主程序
#include "mystruct.h"
int main()
{
MyStruct myStruct;
myStruct.x = 1;
myStruct.f2();
// etc...
}
If, on the other hand, you are trying to share a global instanceof the struct across several compilation units (it's not absolutely clear from your question), do as above but also add
另一方面,如果您尝试在多个编译单元之间共享结构的全局实例(从您的问题中并不是绝对清楚),请按上述方式进行操作,但还要添加
extern MyStruct globalStruct;
to mystruct.h. This will announce that an instance is available with external linkage; in other words that a variable exists but is initialized elsewhere (in your case in mystruct.cpp). Add the initialization of the global instance to mystruct.cpp:
到 mystruct.h。这将宣布一个具有外部链接的实例可用;换句话说,变量存在但在其他地方初始化(在您的情况下在 mystruct.cpp 中)。将全局实例的初始化添加到 mystruct.cpp 中:
MyStruct globalStruct;
MyStruct globalStruct;
This is important. Without manually creating an instance of globalStruct
, you'd get unresolved-externallinker errors. Now you have access to globalStruct
from each compilation unit that includes mystruct.h.
这个很重要。如果不手动创建 的实例globalStruct
,您将收到未解决的外部链接器错误。现在您可以访问globalStruct
包含 mystruct.h 的每个编译单元。
回答by Daniel Wehner
The problem is that you basically have the same code twice as a result if you see an include as just a import of the code.
问题是,如果您将包含视为只是代码的导入,那么您基本上会有两次相同的代码。
You can use #ifdef to fix it, see http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html
您可以使用 #ifdef 来修复它,请参阅http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html
回答by cprogrammer
You should move the common struct to a header file and include that header in both files. Any other solution is a workaround.
您应该将公共结构移动到头文件中,并将该头文件包含在两个文件中。任何其他解决方案都是一种解决方法。
回答by Vern
Declaration and definitions are two different things. For your case, you are allocating space for your structure in main.cpp. In your header, you should use the extern modifier for your struct so that all files that include the header file will look in the global namespace for the structure. Hope it helps.
声明和定义是两个不同的东西。对于您的情况,您正在 main.cpp 中为您的结构分配空间。在您的头文件中,您应该为您的结构使用 extern 修饰符,以便包含头文件的所有文件都将在该结构的全局命名空间中查找。希望能帮助到你。
回答by unkulunkulu
You should define structure in the header file only, you should remove definition from main.cpp
您应该只在头文件中定义结构,您应该从main.cpp
回答by A. K.
May be you can give more information about what is the layout of your project.
也许您可以提供有关项目布局的更多信息。
Going by the guess, probably your problem can be either of the two:
根据猜测,您的问题可能是以下两个之一:
you want forward declaration of struct.
using include guards to prevent redefinition.
你想要结构的前向声明。
使用包含保护来防止重新定义。
See the following link for how to handle both:
有关如何处理两者,请参阅以下链接:
http://www.adp-gmbh.ch/cpp/forward_decl.html
http://www.adp-gmbh.ch/cpp/forward_decl.html
The header files also use include guards, so you can figure out what exactly can solve your problem.
头文件还使用了包含保护,因此您可以弄清楚究竟是什么可以解决您的问题。
回答by kravemir
If you want to share any variable between multiple cpp files, you should declare it in header as extern
. And without extern
in oneof that c++ files.
如果要在多个 cpp 文件之间共享任何变量,则应在标头中将其声明为extern
. 并且没有extern
在其中一个C++ 文件中。
If you don't do it, it'll lack at linking, because multiple objects would have variable with same name. Instead when using extern
one object would have this variable and other objects link it.
如果你不这样做,它将缺乏链接,因为多个对象会有同名的变量。相反,当使用extern
一个对象时,这个变量会被其他对象链接起来。
回答by Ajay
The standard C/C++ approach:
标准的 C/C++ 方法:
// source.h
Put all struct, class, typedef, macro definitions, extern variable declaraltions
// source.cpp
Implement the class, functions, define global/external variables
// main.cpp, and other parts of program
#include"source.h"
回答by Ajay
The header is where you declare what your struct
will consist of (probably a common.h
file included by main.cpp
and myfile.cpp
):
标题是您声明您的struct
遗嘱组成的地方(可能是和common.h
包含的文件):main.cpp
myfile.cpp
struct MyStruct {
int messageID;
int tempVariable;
};
In your main.cpp
, this is where you actually use the struct:
在您的 中main.cpp
,这是您实际使用结构的地方:
void someFunction() {
struct MyStruct tempStruct;
// do something with it
tempStruct.messageID = 1;
}
Don't put the definition of your struct
in both your main.h
and main.cpp
- or you will get a redefinition error!
不要把你的定义struct
同时放在你main.h
和main.cpp
- 否则你会得到一个重新定义错误!
Also, don't include the cpp file - include the header file (e.g. common.h
). Without knowing more about the structure of your program, it is hard to provide better information.
另外,不要包含 cpp 文件 - 包含头文件(例如common.h
)。如果不了解有关程序结构的更多信息,就很难提供更好的信息。