C++ 错误C2011:&x27';:';类别';类型重新定义

C++ 错误C2011:&x27';:';类别';类型重新定义,c++,class,visual-studio-2005,C++,Class,Visual Studio 2005,其中一个头文件如下所示- #include "stdafx.h" class AAA { public: std::string strX; std::string strY; }; 当我试图编译这个项目时,我得到了一个错误 error C2011: 'AAA' : 'class' type redefinition 在我的程序中,没有其他地方我重新定义了类AAA。如何修复此问题?将代码更改为以下内容: #ifndef AAA_HEADER #define AAA_HEAD

其中一个头文件如下所示-

#include "stdafx.h"

class AAA
{
public:
    std::string strX;
    std::string strY;
};
当我试图编译这个项目时,我得到了一个错误

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

在我的程序中,没有其他地方我重新定义了类
AAA
。如何修复此问题?

将代码更改为以下内容:

#ifndef AAA_HEADER
#define AAA_HEADER

#include "stdafx.h"

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

#endif

如果您在某个源文件中多次包含此头文件,则包含保护将强制编译器仅生成一次类,这样它就不会给出
类重新定义
错误。

除了建议的包含保护之外,您还需要将#include“stdafx.h”移出头文件。将其放在cpp文件的顶部。

添加

#pragma once
在AAA.h文件的顶部,应该解决这个问题

像这样

#include "stdafx.h"
#pragma once

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

您需要使用include-guards。AAA\u头是AAA.h文件吗?AAA\u头只是标识文件的唯一字符串。阅读关于这里的守卫,这个解决方案的好处是它将对所有C++编译器工作,而不仅仅是微软的.S.如果它在文件的最上面(在第一线)更不可能包括在一些包含的情况下?