Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++结构定义编译器错误_C++_Compiler Construction_Struct - Fatal编程技术网

C++结构定义编译器错误

C++结构定义编译器错误,c++,compiler-construction,struct,C++,Compiler Construction,Struct,我创建了一个新的.h文件,其中包含以下内容: #include "stdafx.h" #include <string> using namespace std; struct udtCharVec { wstring GraphemeM3; wstring GraphemeM2; }; 当我想要编译它时,编译器告诉我错误C2011:udtCharVec:struct-type-redefinition 我进行了文本搜索,没有在其他任何地方定义struct udt

我创建了一个新的.h文件,其中包含以下内容:

#include "stdafx.h"
#include <string>
using namespace std;

struct udtCharVec
{
    wstring GraphemeM3;
    wstring GraphemeM2;
};
当我想要编译它时,编译器告诉我错误C2011:udtCharVec:struct-type-redefinition

我进行了文本搜索,没有在其他任何地方定义struct udtCharVec


有人知道我哪里出错了吗?

您可能在一个翻译单元中多次包含此头文件。当第二次包含该文件时,已经定义了struct udtCharVec,因此会出现类型重新定义错误

添加一个。第一次包含后,将定义CharVec_H,因此将跳过文件的其余部分:

#ifndef CharVec_H
#define CharVec_H
#include "stdafx.h"
#include <string>
using namespace std

struct udtCharVec
{
    wstring GraphemeM3;
    wstring GraphemeM2;
};
#endif
main.cpp

#include "CharVec.h"
#include "CharMatrix.h"

int main() {
    udtCharMatrix matrix = {};
    CharVec vec = matrix.vec[2];
};
在预处理器运行后,main.cpp看起来像以下标准库:

//#include "CharVec.h":
    #include "stdafx.h"
    #include <string>
    using namespace std

    struct udtCharVec //!!First definition!!
    {
        wstring GraphemeM3;
        wstring GraphemeM2;
    };
//#include "CharMatrix.h":
    //#include "CharVec.h":
        #include "stdafx.h"
        #include <string>
        using namespace std

        struct udtCharVec //!!Second definition!!
        {
            wstring GraphemeM3;
            wstring GraphemeM2;
        };
    struct udtCharMatrix
    {
        CharVec vec[4];
    };

int main() {
    udtCharMatrix matrix = {};
    CharVec vec = matrix.vec[2];
};

此扩展文件包括struct udtCharVec的两个定义。如果在CharVec.h中添加include-guard,预处理器将删除第二个定义。

您可能在单个翻译单元中多次包含此头文件。当第二次包含该文件时,已经定义了struct udtCharVec,因此会出现类型重新定义错误

添加一个。第一次包含后,将定义CharVec_H,因此将跳过文件的其余部分:

#ifndef CharVec_H
#define CharVec_H
#include "stdafx.h"
#include <string>
using namespace std

struct udtCharVec
{
    wstring GraphemeM3;
    wstring GraphemeM2;
};
#endif
main.cpp

#include "CharVec.h"
#include "CharMatrix.h"

int main() {
    udtCharMatrix matrix = {};
    CharVec vec = matrix.vec[2];
};
在预处理器运行后,main.cpp看起来像以下标准库:

//#include "CharVec.h":
    #include "stdafx.h"
    #include <string>
    using namespace std

    struct udtCharVec //!!First definition!!
    {
        wstring GraphemeM3;
        wstring GraphemeM2;
    };
//#include "CharMatrix.h":
    //#include "CharVec.h":
        #include "stdafx.h"
        #include <string>
        using namespace std

        struct udtCharVec //!!Second definition!!
        {
            wstring GraphemeM3;
            wstring GraphemeM2;
        };
    struct udtCharMatrix
    {
        CharVec vec[4];
    };

int main() {
    udtCharMatrix matrix = {};
    CharVec vec = matrix.vec[2];
};

此扩展文件包括struct udtCharVec的两个定义。如果将include guard添加到CharVec.h,预处理器将删除第二个定义。

通常,类似的问题会在输出窗格中显示其他信息,而错误列表只会显示第一行,您可以单击转到前一个定义

如果文件位于同一位置,那么确实会多次包含该文件-可以在C++/advanced下打开Show includes(显示包含)选项,以便在发生时查看列出的所有包含

大多数.h文件应包含一次guards或pragma,以避免此类错误


此外,您不应将stdafx.h包含在头文件中-这应在.cpp文件次优开始时完成,或在项目中指定为强制包含。

通常,类似的问题在输出窗格中有附加信息,而错误列表仅获取第一行,您可以单击转到以前的定义

如果文件位于同一位置,那么确实会多次包含该文件-可以在C++/advanced下打开Show includes(显示包含)选项,以便在发生时查看列出的所有包含

大多数.h文件应包含一次guards或pragma,以避免此类错误


此外,您不应将stdafx.h包含在头文件中-这应在.cpp文件次优开始时完成,或在项目中指定为强制包含。

将此宏添加到头文件的顶部

#pragma once

将此宏添加到头文件的顶部

#pragma once

您在定义结构的当前文件中出错?或者您是否将此代码用作其他文件的include文件?除了您的问题之外,不要在头文件中使用using指令:头文件的使用代码在哪里?编辑您的问题并发布…您在定义结构的当前文件中遇到错误?或者您是否将此代码用作其他文件的include文件?除了您的问题之外,不要在头文件中使用using指令:头文件的使用代码在哪里?编辑您的问题并发布…谢谢。它奏效了,但我不明白为什么。您在单个翻译单元中多次包含此头文件是什么意思?@user2421725表示您在程序中包含多次,这导致CharVec_H尝试定义两次,因此出现错误从头中删除using namespace std是明智的。谢谢。它奏效了,但我不明白为什么。您在单个翻译单元中多次包含此头文件是什么意思?@user2421725表示您在程序中包含多次,这导致CharVec_H尝试定义两次,因此出现错误从头中删除using namespace std是明智的。