Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
模板类类型转换 我正在学习C++中的模板,我正在尝试编写一个模板类来处理不同的数据类型,以便读取一种类似于格式的配置文本文件。_C++_Templates - Fatal编程技术网

模板类类型转换 我正在学习C++中的模板,我正在尝试编写一个模板类来处理不同的数据类型,以便读取一种类似于格式的配置文本文件。

模板类类型转换 我正在学习C++中的模板,我正在尝试编写一个模板类来处理不同的数据类型,以便读取一种类似于格式的配置文本文件。,c++,templates,C++,Templates,类型,默认值 我定义了以下类 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>

类型,默认值

我定义了以下类

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;
}
我得到一个编译错误“从'std::uu 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();
}
类选项\u t
{
公众:
选项_t();
//~options_t();
模板
无效设置选项(选项t选项);
私人:
};
选项:选项
{
//读取文件并根据_类型创建特定的选项对象
std::string _type=“integer”;
std::string _defaultValue=“5”;
如果(_type.compare(“整数”)==0)
{
选项\u t选项(\u类型,\u默认值);
设置_选项(选项);
}
else if(_type.compare(“real”)==0)
{
选项\u t选项(\u类型,\u默认值);
设置_选项(选项);
}
else if(_type.compare(“boolean”)==0)
{
选项\u t选项(\u类型,\u默认值);
设置_选项(选项);
}
else if(_type.compare(“string”)==0)
{
选项\u t选项(\u类型,\u默认值);
设置_选项(选项);
}
其他的
{

//LOG(ERROR)取决于您想做什么。我假设
int tmpInt
是正确的


要检索
int
选项必须是
option\u t
option\u t
,其中
t
可转换为
int
。看起来像是在尝试使用字符串。

添加完整的代码。
选项
是什么?选项是类对象:option\u t选项(“整数”,“5”);应该有效。完成后(但最少)复制问题的代码。好的,我将复制粘贴代码的其余部分,而不仅仅是“rest”。编译的东西(除了有问题的行)。
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();
}