错误:没有匹配函数来调用'to_string(std :: basic_string <char>&)'

ar2*_*015 4 c++ string compiler-errors c++11

即使在模板中我可以有任何类型,该函数to_string也不适用于基本字符串:

例如:

std::string str("my string");
my_class(str);
Run Code Online (Sandbox Code Playgroud)

使用此仿函数定义:

template<class valuetype>
void operator()(valuetype value)
{
    ...
    private_string_field = std::to_string(value);
Run Code Online (Sandbox Code Playgroud)

不起作用.这是错误:

错误:没有匹配函数来调用'to_string(std :: basic_string&)'

什么是避免它的最佳方法.

我提前请求避免链接到不相关的问题只是因为一些常见的关键字.

Dre*_*ann 6

std::to_string 仅适用于基本数字类型.

如果您需要更通用的功能,boost::lexical_cast可以使用更多类型 - 实际上可以发送到任何类型的任何类型iostream.

#include <boost/lexical_cast.hpp>

...

private_string_field = boost::lexical_cast<std::string>(value);
Run Code Online (Sandbox Code Playgroud)