7

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

命名空间(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;
};
4

4 回答 4

30

包括警卫:

#ifndef FILE_H
#define FILE_H

//file contents here

#endif

出于这个确切原因,头文件应该包含保护 - 同一翻译单元中的多个包含可能导致多重定义。

另一种方法是使用

#pragma once

但这不是所有编译器都支持的。

于 2013-01-04T20:31:49.293 回答
0

如果其他人面临这种​​情况,则可能是库包含在项目的属性中,并且该库中的头文件包含在项目文件中。

于 2015-03-31T12:23:11.373 回答
0

不正确的前向声明

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

File1.h

namespace MyNamespace { enum NotActuallyAnEnum; }

File2.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; }
于 2015-04-21T00:12:12.727 回答
0

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 Project1 and Project2 and you let a symlink called Project point to either Project1 or Project2.

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

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 Project would open either Project1 or Project2 depending on the symlink. The symlink could be removed/created by some simple mklink_1 and mklink_2 like script files.

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!

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

于 2017-10-24T13:41:10.247 回答