2014-12-06 154 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。看起来你喜欢你试图使用一个字符串。