如何使用我的头文件中某处声明的枚举?

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

我是 C++ 的初级用户,从昨天开始我就遇到了这个问题。 我有两个头文件: 'Builder.hpp' 其中包括一些枚举和结构的声明和定义:

#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:

#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

两个头文件中的 include guard 是 BUILDER_HPP。您需要为每个文件使用一个唯一的文件(这就是为什么它通常是文件名)。

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