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
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.
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
Two common solutions.
v2 may overflow.
include<string>
c++11 provides std::to_string for string conversion
Or as mentioned above
According to the original poster’s procedure, to_string conversion is more efficient.
It is recommended to return const string