Code:
string str(double dX, double dY)//Convert vector coordinates to string, the format is "(x=, y=)"
{
return "(x=" + dX + ", y=" + dY + ")";
}
Error message:
Expression must contain an integer or an unscoped enumeration type
我想大声告诉你2017-06-05 11:13:01
Two common solutions.
std::string v1(double dX, double dY) {
std::ostringstream stream;
stream << "(x=" << dX << ", y=" << dY << ")";
return stream.str();
}
std::string v2(double dX, double dY) {
char buff[1024];
sprintf(buff, "(x=%f, y=%f)", dX, dY);
return buff;
}
v2 may overflow.
大家讲道理2017-06-05 11:13:01
c++11 provides std::to_string for string conversion
Or as mentioned above
std::string v1(double dX, double dY) {
std::ostringstream stream;
stream << "(x=" << dX << ", y=" << dY << ")";
return stream.str();
}
According to the original poster’s procedure, to_string conversion is more efficient.
It is recommended to return const string