将双点坐标转换为字符串
Convert double point coordinates into string
string Point::ToString(const Point& pt)
{
std::stringstream buffX; //"Incomplete type is not allowed"????
buffX << pt.GetX(); // no operator "<<" matches these operands????
std::stringstream buffY;
buffY << pt.GetY();
string temp = "Point(" + buffX + ", " + buffY + ")"; //???....how to combine a couple of strings into one?..
return temp.str();
}
我遵循了类似问题中的代码,但系统显示"不允许不完整的类型"---buffX 下的红线
"<<"下的红线也表示----没有运算符"<<"与这些操作数匹配
真的不知道为什么..
谢谢!
您需要#include <sstream>
才能使用std::ostringstream
。
然后:
std::string Point::ToString(const Point& pt)
{
std::ostringstream temp;
temp << "Point(" << pt.GetX() << ", " << pt.GetY() << ")";
return temp.str();
}
不清楚你为什么要传入一个Point
,因为这是该类的成员。也许更清洁的是:
std::string Point::ToString() const
{
std::ostringstream temp;
temp << "Point(" << GetX() << ", " << GetY() << ")";
return temp.str();
}
这也许是错误的,假设GetX()
和GetY()
return
某种数字类型(int
,float
,double
,...(。如果不是这种情况,您可能希望更改它们(最小惊讶原则(或直接访问class
的基础数据成员。
如果你正在为这种编译器错误而苦苦挣扎,我强烈建议你给自己一本好C++书。
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 如何将这个std::字符串转换为std::基本字符串
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 如何在C++中将十六进制字符串转换为文本数据
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 如何从字符串转换为双精度*
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 如何在C++中将 Python 字符串转换为其转义版本?
- 如何在 C++11 中将标准::字符串转换为标准::u32字符串?
- 如何将整数字符串转换为整数的二维向量?
- 使用 "stringstream " 将字符串转换为整数
- 将字符串转换为浮点数或整数,而无需使用内置函数(如 atoi 或 atof)
- 将文件复制到自定义位置,存在字符串转换问题
- 将ANSI C字符串转换为UNICODE
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将字符串转换为无符号字符数组/字节数组