sara
sara

Reputation: 3934

String to Enum template error

I need to convert a String to enum.

I followed the idea in String to enum in C++

And this is my code:

#ifndef ENUMPARSER_H
#define ENUMPARSER_H
#include <iostream>
#include <map>
#include <string>

enum MYENUM
{
    VAL1,
    VAL2
};
using namespace std;
template <typename T>
class EnumParser
{
    map<string, T> enumMap;
public:
    EnumParser();
    T ParseEnum(const string &value)
    {
        map<string, T>::const_iterator iValue= enumMap.find(value);
        if(iValue!=enumMap.end())
            return iValue->second;
    }

};

EnumParser<MYENUM>::EnumParser()
{
    enumMap["VAL1"] = VAL1;
    enumMap["VAL2"] = VAL2;
}

#endif // ENUMPARSER_H

When tring to compile it I get the following errors: enter image description here

I'm working on QT 4.8.

What is my mistake?

Upvotes: 1

Views: 531

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

map<string, T>::const_iterator is a dependent name, you need:

 typename map<string, T>::const_iterator iValue= enumMap.find(value);

You can read more about the subject here.

Upvotes: 2

Related Questions