error: no matching function for call to ‘to_string(std::basic_string<char>&)’
error: no matching function for call to ‘to_string(std::basic_string<char>&)’
即使在模板中我可以有任何类型,函数 to_string
不适用于基本字符串:
例如:
std::string str("my string");
my_class(str);
用这个函子定义:
template<class valuetype>
void operator()(valuetype value)
{
...
private_string_field = std::to_string(value);
不起作用。这是错误:
error: no matching function for call to
‘to_string(std::basic_string&)’
避免它的最佳方法是什么。
提前要求不要因为几个常用关键词就链接到不相关的问题。
基本字符串没有to_string
。那就没关系了。
根据Benjamin Lindley的建议我会考虑以下设计,使用to_string
但提供默认模板:
#include <iostream>
#include <string>
struct Type {
explicit operator std::string() const{
return std::string("I am type");
}
};
namespace std {
template <typename T>
string to_string(const T& value) {
return string(value);
}
}
int main(int argc, char **argv) {
// this is what would be in class
Type x;
std::string private_string_field;
private_string_field = std::to_string(42);
std::cout << private_string_field << std::endl;
private_string_field = std::to_string(x);
std::cout << private_string_field << std::endl;
return 0;
}
默认情况下,它会尝试将操作数转换为字符串。这样自定义类型可以提供自己的转换。替代设计是在内部使用 stringstream
和 operator<<
进行转换,如下所示:
#include <iostream>
#include <string>
#include <sstream>
struct Type {
friend std::ostream& operator<<(std::ostream& out, const Type& value){
return out << "Type through operator<<";
}
};
template <class T>
std::string to_str(const T& value) {
std::string ret;
std::ostringstream ss;
ss << value;
ret = ss.str();
return ret;
};
int main(int argc, char **argv) {
// this is what would be in class
Type x;
std::string private_string_field;
private_string_field = to_str(42);
std::cout << private_string_field << std::endl;
private_string_field = to_str(x);
std::cout << private_string_field << std::endl;
return 0;
}
std::to_string
仅适用于基本数字类型。
如果您需要更通用的函数,boost::lexical_cast
将适用于更多类型 - 实际上可以发送到 iostream
.
的任何类型
#include <boost/lexical_cast.hpp>
...
private_string_field = boost::lexical_cast<std::string>(value);
即使在模板中我可以有任何类型,函数 to_string
不适用于基本字符串:
例如:
std::string str("my string");
my_class(str);
用这个函子定义:
template<class valuetype>
void operator()(valuetype value)
{
...
private_string_field = std::to_string(value);
不起作用。这是错误:
error: no matching function for call to ‘to_string(std::basic_string&)’
避免它的最佳方法是什么。
提前要求不要因为几个常用关键词就链接到不相关的问题。
基本字符串没有to_string
。那就没关系了。
根据Benjamin Lindley的建议我会考虑以下设计,使用to_string
但提供默认模板:
#include <iostream>
#include <string>
struct Type {
explicit operator std::string() const{
return std::string("I am type");
}
};
namespace std {
template <typename T>
string to_string(const T& value) {
return string(value);
}
}
int main(int argc, char **argv) {
// this is what would be in class
Type x;
std::string private_string_field;
private_string_field = std::to_string(42);
std::cout << private_string_field << std::endl;
private_string_field = std::to_string(x);
std::cout << private_string_field << std::endl;
return 0;
}
默认情况下,它会尝试将操作数转换为字符串。这样自定义类型可以提供自己的转换。替代设计是在内部使用 stringstream
和 operator<<
进行转换,如下所示:
#include <iostream>
#include <string>
#include <sstream>
struct Type {
friend std::ostream& operator<<(std::ostream& out, const Type& value){
return out << "Type through operator<<";
}
};
template <class T>
std::string to_str(const T& value) {
std::string ret;
std::ostringstream ss;
ss << value;
ret = ss.str();
return ret;
};
int main(int argc, char **argv) {
// this is what would be in class
Type x;
std::string private_string_field;
private_string_field = to_str(42);
std::cout << private_string_field << std::endl;
private_string_field = to_str(x);
std::cout << private_string_field << std::endl;
return 0;
}
std::to_string
仅适用于基本数字类型。
如果您需要更通用的函数,boost::lexical_cast
将适用于更多类型 - 实际上可以发送到 iostream
.
#include <boost/lexical_cast.hpp>
...
private_string_field = boost::lexical_cast<std::string>(value);