C++ 错误 C2011: '': 'class' 类型重新定义

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

error C2011: '' : 'class' type redefinition

c++classvisual-studio-2005

提问by user3164272

One of the header files is as follows -

其中一个头文件如下——

#include "stdafx.h"

class AAA
{
public:
    std::string strX;
    std::string strY;
};

When I try to compile the project, I get the error

当我尝试编译项目时,出现错误

error C2011: 'AAA' : 'class' type redefinition

Nowhere else in my program have I redefined the class AAA. How do I fix this?

在我的程序中没有其他地方我重新定义了 class AAA。我该如何解决?

回答by Ashot

Change to code to something like this:

将代码更改为如下所示:

#ifndef AAA_HEADER
#define AAA_HEADER

#include "stdafx.h"

class AAA
{
public:
    std::string strX;
    std::string strY;
};

#endif

If you include this header file more than once in some source file, include guards will force compiler to generate class only once so it will not give class redefinitionerror.

如果在某个源文件中多次包含此头文件,包含保护将强制编译器仅生成一次类,因此不会class redefinition出错。

回答by Kevin Johnsrude

Adding

添加

#pragma once

to the top of your AAA.h file should take care of the problem.

到 AAA.h 文件的顶部应该可以解决这个问题。

like this

像这样

#include "stdafx.h"
#pragma once

class AAA
{
public:
    std::string strX;
    std::string strY;
};

回答by ScottMcP-MVP

In addition to the suggested include guards you need to move #include "stdafx.h" out of the header. Put it at the top of the cpp file.

除了建议的包含保护之外,您还需要将 #include "stdafx.h" 移出标题。把它放在cpp文件的顶部。