samedi 16 février 2019

How to access an C++11 enum class from another file?

I have a header file that will eventually include more than one enum class. However, when I include the header file in another file and try to use the enum class, my program will not compile. For example:

enums.h:

#ifndef ENUMS_H
#define ENUMS_H

enum class TokenType : char
{
     IDEN,
     STRING,
     SEMICO
};

#endif

and main.cpp:

#include <iostream>
#include "enums.h"

int main()
{
     char token = TokenType::STRING; //Does not compile!
}

However, when I use a regular enum, it compiles correctly:

enums.h:

#ifndef ENUMS_H
#define ENUMS_H

enum TokenType : char
{
     IDEN,
     STRING,
     SEMICO
}

#endif

and main.cpp:

#include <iostream>
#include "enums.h"

int main()
{
     char token = STRING; //This does compile!
}

Does anyone know how to do this correctly? I've searched a lot and came up with nothing.

Aucun commentaire:

Enregistrer un commentaire