C++ GCC 编译器错误:“重新定义...先前定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/707920/
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
GCC compiler error: "redefinition...previously defined"
提问by Brian R. Bondy
I'm getting a lot of " redefinition of x....x previously defined here". Please what does this error means?
我得到了很多“以前在这里定义的 x....x 的重新定义”。请问这个错误是什么意思?
回答by Brian R. Bondy
You need to limit each file from being included only once. You can do this in 2 ways.
您需要限制每个文件只包含一次。您可以通过两种方式执行此操作。
1) At the top of your header files put:
1)在头文件的顶部放置:
#pragma once
Or 2) if your compiler doesn't support that, put at the top/end of your header files:
或 2) 如果您的编译器不支持,请将其放在头文件的顶部/末尾:
#ifndef _MYFILE_H_
#define _MYFILE_H_
...
#endif
Replace MYFILE with the name of your file, and replace ... with the contents of the header file.
将 MYFILE 替换为您的文件名,并将 ... 替换为头文件的内容。
回答by Andrew Edgecombe
The error means that there is a symbol that has been defined in one place and an alternate definition has been made in another place.
该错误意味着在一个地方定义了一个符号,而在另一个地方进行了替代定义。
This can occur if in cases like:
在以下情况下可能会发生这种情况:
- if you define two functions with the same name
- if there is a mismatch between a function and it's prototype
- you call a non-trivial function before it has been defined, and without a prototype
- 如果你定义了两个同名的函数
- 如果函数与其原型之间存在不匹配
- 在定义之前调用一个非平凡的函数,并且没有原型
In this last case there will be a mismatch between the real function and the "implicit declaration" that the compiler assumes when it doesn't have a prototype to use.
在最后一种情况下,实际函数与编译器在没有原型可供使用时假定的“隐式声明”之间将不匹配。
These situations can be avoided by:
这些情况可以通过以下方式避免:
- Ensuring that function prototypes are only declared once
- Ensuring that all functions have unique names within their scope (ie. within a file if they are
static
, or unique if they are used between object files) - Be careful if using
extern
statements in source files to declare prototypes. Better to use a prototype from the appropriate header file. - Ensure that all functions have prototypes - either within the source file in the case of
static
functions, or in a header file if they are to be used by other object files. - Ensure that all header files can only be included once for each source file, by using either of the constructs suggested by Mehrdad and Brian R. Bondy
- 确保函数原型只声明一次
- 确保所有函数在其范围内具有唯一的名称(即,如果它们是在文件中
static
,或者如果它们在目标文件之间使用则是唯一的) - 如果
extern
在源文件中使用语句来声明原型,请小心。最好使用来自相应头文件的原型。 - 确保所有函数都有原型——如果是
static
函数,要么在源文件中,要么在其他目标文件使用的头文件中。 - 通过使用 Mehrdad 和 Brian R. Bondy 建议的结构,确保每个源文件只能包含一次所有头文件
回答by Mehrdad Afshari
You are probably including a header file twice. Make sure your header files are surrounded by #ifndef
statements.
您可能包含两次头文件。确保您的头文件被#ifndef
语句包围。
http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html
http://www.fredosaurus.com/notes-cpp/preprocessor/ifdef.html
回答by MasterMastic
The same thing just happened to me and it was because I accidentally included the .c/.cpp file (within it) instead of the header file.
同样的事情发生在我身上,这是因为我不小心包含了 .c/.cpp 文件(在其中)而不是头文件。
That would definitely get you a lot of that error.
那肯定会让你犯很多错误。