关于c ++:如何使用在我的头文件中声明的枚举?

How to use an enum declared somewhere in my header file?

我是C ++的初学者,自昨天开始遇到此问题。
我有两个头文件:
'Builder.hpp',其中包括一些枚举和结构的声明和定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef BUILDER_HPP
    #define BUILDER_HPP

    #ifdef _DEFAULT_INCLUDES
        #include <AsDefault.h>
    #endif

    #include"../EcoLibs/EcoComLib/OpcUaCom.hpp"
    #include"LineCoordEngine.hpp"

    //Builder class help types
    enum BuildOpcUaType_enum
    {
      //Some stuff
    };

    enum BuildVariableTypes_enum
    {
        //Some stuff
    };

    struct BuildOpcUaLists_type
    {
        //Some stuff
    };

    //Builder class
    class Builder
    {
        public:
            Builder();
            ~Builder();

            Machine *BuildOpcUaMachine(char serverUrl[UA_MAX_STRING], BuildOpcUaLists_type *lists, BuildOpcUaType_enum uaType);
            DataExchanger *BuildDataExchanger(unsigned short int machineIndex, unsigned short int machineTypeIndex);

        private:
            void CreateOpcUaList(//stuff);                      
            void CreateCharNumber(//stuff);
            //Private variables declaration
    };

#endif

第二个头文件是:" Parser.hpp"。
我想声明一个在'Builder.hpp'中定义的'BuildOpcUaType_enum'类型的变量。 我在" Parser.hpp"中包含了" Builder.hpp",但仍然收到错误消息:
BuildOpcUaType_enum没有命名类型。

Parser.hpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef BUILDER_HPP
#define BUILDER_HPP

#include"Builder.hpp"
#include <string>

using namespace std;

struct Attributes{
    string name;
    string value;
};

BuildOpcUaType_enum en;
class Parser{
    private:
        //Variables:
        Attributes attributes[10];
        unsigned int BufferIds[200];
        string connectionStrings[20];
        unsigned long int nFileLength, nOpenFileIdent, nXMLReaderIdent;
        unsigned short int length, noOfOpenedStructs, readListIndex, writeListIndex, readListDestIndex, writeListSrcIndex;
        string comType, sErrorMessage, sStatusText, sXMLElementName, sXMLElementValue;
        string structNameAttValues[10];
        unsigned int TagId_Read[200];
        unsigned int TagId_Write[200];
        unsigned short int xmlData[10000];

        //Boolean variables:
        bool isArrayTag, isBufferIdTag, isDatatypeTag, isNameTag, bStart, isTagIdTag;

        //Constants:
        string sFilePath;
        string sDeviceName;

        //The rest?
        BuildOpcUaType_enum en;

    public:
        Parser();
        ~Parser();
};
#endif

您在两个头文件中的包含保护均为BUILDER_HPP。 您需要为每个文件使用唯一的文件名(这就是为什么它通常是文件名)。

就目前而言,您实际上并没有包含Builder.hpp中的任何内容,因为#define BUILDER_HPP发生在Parser.hpp中的#include"Builder.hpp"之前,因此包含保护#ifndef BUILDER_HPP的值为false。