Search code examples
c++header-files

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


I am beginner user of C++ and I am having this problem since yesterday. I have two header files: 'Builder.hpp' which includes declaration and definition of some enums and structs:

#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

The second header file is: 'Parser.hpp'. I want to declare a variable of type 'BuildOpcUaType_enum' which is defined in 'Builder.hpp'. I included 'Builder.hpp' in 'Parser.hpp', but still getting an error saying: BuildOpcUaType_enum does not name a type.

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

Solution

  • Your include guard in both header files is BUILDER_HPP. You need to use a unique one for each file (that's why it is usually the filename).

    As it stands, you are not actually including anything from Builder.hpp, because #define BUILDER_HPP happens before #include "Builder.hpp" in Parser.hpp and so the include guard #ifndef BUILDER_HPP evaluates to false.