2014-12-06 139 views
0

我學習的模板在C++,我試着寫一個模板類,以讀取類似模板類類型轉換

TYPE的方式格式化的配置文本文件來處理不同的數據類型, DEFAULT_VALUE

我定義了下面的類

template <class T> 
class option_t 
{ 
public: 

    option_t(std::string _type, std::string _defaultValue); 

    //~option_t(); 

    std::string get_type(); 

    T get_defaultValue(); 

private: 

    T defaultValue; 
}; 

template <class T> 
option_t<T>::option_t(std::string _type,std::string _defaultValue) 
{ 
    type = _type; 

    if(type.compare("integer") == 0) 
    { 
    defaultValue = std::stoi(_defaultValue); 
    } 
    else if(type.compare("real") == 0) 
    { 
    char *pEnd; 
    defaultValue = std::strtod(_defaultValue.c_str(),&pEnd); 
    } 
    else if(type.compare("boolean") == 0) 
    { 
    std::transform(_defaultValue.begin(),_defaultValue.end(),_defaultValue.begin(),::tolower); 
    if(_defaultValue.compare("true") == 0 || 
     _defaultValue.compare("1") == 0 || 
     _defaultValue.compare("on") == 0) 
    { 
     defaultValue = true; 
    } 
    else 
    { 
     defaultValue = false; 
    } 
    } 
    else 
    { 
    //LOG(ERROR) << "Option " << name << " : unknown data type (" << type << ")"; 
    } 

template <class T> 
std::string option_t<T>::get_type() 
{ 
    return type; 
} 

template <class T> 
T option_t<T>::get_defaultValue() 
{ 
    return defaultValue; 
} 

,當我使用下面的行到我的主代碼

int tmpInt = option.get_defaultValue(); 

我收到一個彙編錯誤「沒有可行的從'std :: __ 1 :: basic_string'轉換爲'int'」

這是什麼意思?我該如何解決它?

感謝和抱歉:-)

這裏我的代碼

class options_t 
{ 
    public: 
    options_t(); 
    //~options_t(); 

    template <class T> 
    void set_option(option_t<T> option); 

    private: 
}; 

options_t::options_t() 
{ 
    // read file and depending on _type create a specific option object 
    std::string _type = "integer"; 
    std::string _defaultValue = "5"; 

    if(_type.compare("integer") == 0) 
    { 
    option_t<int> option(_type,_defaultValue); 
    set_option(option); 
    } 
    else if(_type.compare("real") == 0) 
    { 
    option_t<double> option(_type,_defaultValue); 
    set_option(option); 
    } 
    else if(_type.compare("boolean") == 0) 
    { 
    option_t<bool> option(_type,_defaultValue); 
    set_option(option); 
    } 
    else if(_type.compare("string") == 0) 
    { 
    option_t<std::string> option(_type,_defaultValue); 
    set_option(option); 
    } 
    else 
    { 
    // LOG(ERROR) << " invalid data type(" << _type << ")"; 
    } 
} 


template <class T> 
void options_t::set_option(option_t<T> option) 
{ 
    std::string _type = option.get_type(); 

    if(_type.compare("integer") == 0) 
    { 
    int tmpInt = option.get_defaultValue(); 

    option_t<int> tmpOption(option.get_type(),defaultValue); 
    } 
    else if(_type.compare("real") == 0) 
    { 
    //todo; 
    } 
    else if(_type.compare("boolean") == 0) 
    { 
    //todo; 
    } 
    else if(_type.compare("string") == 0) 
    { 
    //todo; 
    } 
    else 
    { 
    // LOG(ERROR) << " invalid data type(" << option.get_type() << " )"; 
    } 
} 

int main() 
{ 
    options_t options(); 
} 
+0

添加完整的代碼。什麼是「選項」? – 2014-12-06 15:56:27

+0

選項是一個類對象:option_t選項(「integer」,「5」); – alecsphys 2014-12-06 16:27:53

+0

應該工作。發佈重現問題的完整(但最少)代碼。 – 2014-12-06 16:31:56

回答

0

其餘所有的愚蠢的問題,取決於你想要做什麼。我會假設int tmpInt是正確的。

要檢索一個intoption必須是option_t<int>option_t<T>其中T可轉換爲int。看起來你喜歡你試圖使用一個字符串。