error C2011: '' : 'class' type redefinition

前端 未结 3 1497
情歌与酒
情歌与酒 2020-12-31 01:40

One of the header files is as follows -

#include \"stdafx.h\"

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

When I tr

相关标签:
3条回答
  • 2020-12-31 01:51

    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 redefinition error.

    0 讨论(0)
  • 2020-12-31 01:55

    Adding

    #pragma once
    

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

    like this

    #include "stdafx.h"
    #pragma once
    
    class AAA
    {
    public:
        std::string strX;
        std::string strY;
    };
    
    0 讨论(0)
  • 2020-12-31 02:04

    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.

    0 讨论(0)
提交回复
热议问题