Я изучаю шаблоны в 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();
}
Зависит от того, что вы хотите сделать. Я буду считать, что int tmpInt
верен.
Чтобы получить int
, option
должен быть option_t<int>
или option_t<T>
где T
можно конвертировать в int
. Посмотрите, как вы пытаетесь использовать строку.
option
?