2012-07-04 241 views
1

我需要將字符串轉換爲枚舉。字符串到枚舉模板錯誤

我跟着String to enum in C++

的想法,這是我的代碼:

#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 

當特林編譯它,我得到以下錯誤: enter image description here

我的工作QT 4.8。

我的錯誤是什麼?

+0

typename map :: const_iterator iValue = enumMap.find(value); const_iterator是相關名稱。 – ForEveR

回答

2

map<string, T>::const_iterator是一個從屬名稱,您需要:

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

你可以閱讀更多有關主題here

+0

我仍然得到最後一個錯誤(太少的模板參數列表) – sara

+0

當你專門爲MYENUM的構造函數,你有c-tor之前的模板<>? – ForEveR

+0

@ForEveR你是對的,那是我如何修復它... – sara