C++ 错误 C2011:“XX”:“类”类型重新定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14164472/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:03:24  来源:igfitidea点击:

Error C2011: 'XX' : 'class' type redefinition

c++class

提问by Pacha

I have this compiler error (C2011) with this piece of code. I don't know what is wrong with it.

这段代码有这个编译器错误(C2011)。我不知道这有什么问题。

The namespace (Ogre) doesn't have a definition for PlaneMovement. I also tried a different name and still the same errors.

命名空间 (Ogre) 没有PlaneMovement. 我也尝试了不同的名称,但仍然出现相同的错误。

#include <Ogre.h>

using namespace Ogre;

class PlaneMovement
{
public:
    PlaneMovement(Degree startingAngle, Real velocity = 2, Real gravity = 2);
    Vector2 updateMovement(const FrameEvent& evt);
private:
    Degree currentAngle;
    Real currentVelocityX;
    Real currentVelocityY;
    Real gravity;
    bool top;
};

回答by Luchian Grigore

Include guards:

包括守卫:

#ifndef FILE_H
#define FILE_H

//file contents here

#endif

Header files should have include guards for this exact reason - multiple inclusion in the same translation unit can lead to a multiple definition.

出于这个确切原因,头文件应该包含保护——在同一个翻译单元中多次包含会导致多个定义。

The alternative is using

替代方法是使用

#pragma once

but this isn't supported by all compilers.

但这并非所有编译器都支持。

回答by Vladislav Makarov

If someone else faces this situation, it may be when library includes in property of project and header files from this library include in projects file.

如果其他人遇到这种情况,可能是库包含在项目的属性中,而该库中的头文件包含在项目文件中。

回答by sgryzko

Incorrect Forward Declaration

不正确的前向声明

Another possible cause, if you're a goof like me, could be that you used enuminstead of classwhen forward declaring a class.

另一个可能的原因,如果你像我一样是个傻瓜,可能是你在前向声明 a 时使用了enum而不是。classclass

File1.h

文件1.h

namespace MyNamespace { enum NotActuallyAnEnum; }

File2.h

文件2.h

class NotActuallyAnEnum

{
...
}

This will produce something like the following error:

这将产生类似于以下错误的内容:

error C2011: 'enum' type redefinition

Obviously the fix is to correct the forward declaration:

显然,解决方法是纠正前向声明:

namespace MyNamespace { class NotActuallyAnEnum; }

回答by E. van Putten

You might also get this error if you have multiple branches of your project on your development station and use a symlink to point to one of them.

如果您的开发站上有多个项目分支并使用符号链接指向其中之一,您也可能会收到此错误。

Let's suppose you have two different branches of your solution called Project1and Project2and you let a symlink called Projectpoint to either Project1or Project2.

假设您的解决方案有两个不同的分支,称为Project1and,Project2并且您让一个名为的符号链接Project指向Project1or 或Project2

The idea is that you could switch between branches and the project would always appear to be Projectto your application and possibly some other tools that expect it there.

这个想法是您可以在分支之间切换,并且项目将始终出现Project在您的应用程序中,可能还有其他一些期望它出现在那里的工具。

Disclaimer: yes, version control can switch between branches, but this way you will not have to rebuild the whole application every single time you switch branches. Besides both branches can still be under version control.

免责声明:是的,版本控制可以在分支之间切换,但这样您就不必每次切换分支时都重新构建整个应用程序。此外两个分支仍然可以在版本控制之下。

Okay, so opening Projectwould open either Project1or Project2depending on the symlink. The symlink could be removed/created by some simple mklink_1and mklink_2like script files.

好的,所以打开Project将打开Project1Project2取决于符号链接。符号链接可以被删除/由一些简单的创建mklink_1mklink_2类似脚本文件。

Here comes the pitfall:

陷阱来了:

If you don't pay attention and directly open the solution at location 1 or 2 directly (instead of following the directory symlink with Visual Studio), the pre-processor might be tricked into mixing Project1\MyHeader.h(or MyProject2\MyHeader.h) with MyProject\MyHeader.h!

如果你不注意,在位置1或2直接打开该解决方案直接(而不是在与Visual Studio中的目录符号链接),预处理器可能被欺骗混合Project1\MyHeader.h(或MyProject2\MyHeader.h)有MyProject\MyHeader.h

Even those are technically the same file, the preprocessor doesn't know about the symlink. So here the #pragma oncewould not save you!

即使这些在技术上是相同的文件,预处理器也不知道符号链接。所以这里#pragma once不会救你!