如何打印职位
How to print the position
由于x_str和y_str是本地的,所以我在这个函数中没有得到正确的输出。(非法字符打印在x_str和y_str的位置)我不想在我的类中再添加两个成员变量x_str,y_str。
因此,可以用什么来代替这个函数来获得正确的输出。
string Pos::getPosReport(){
string x_str;
x_str = x;
string y_str;
y_str = y;
return string("(" + x_str + "," + y_str + ")" );
}
编辑:
class Pos {
int x;
int y;
public:
Pos();
Pos(Pos const&);
Pos(int,int);
Pos& operator=(Pos const&);
bool operator==(Pos const&);
bool operator!=(Pos const&);
void setPos(Pos const&);
void setPos(int,int);
void setx(int);
void sety(int);
int getx() const ;
int gety() const ;
string getPosReport();
virtual ~Pos();
};
std::stringstream ss;
ss << "(" << x << "," << y << ")";
return ss;
(这是整个功能体)。
问题不在于x_str
和y_str
是局部变量,而在于赋值没有达到预期的效果。也就是说,不能将int
转换为string
。
您可以使用_itoa()
将int
转换为char*
或stringstream
,如Michael的回答所示。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 如何使用重新定义的打印函数打印Lua表?
- 在 POS 打印机上打印条形码字体
- 视觉打印没有打印任何输出内容?C++ SDL
- 打印,打印到MAC OS X上
- 为类打印输出(打印调试信息)的最佳实践是什么?
- QWebView不在实际打印机上打印多页
- 有没有办法创建一个公共输出流对象以在控制台上打印并打印到 c++ 中的文件
- 防止在打印C++打印中打印标志
- <string> 当我得到一个'compilr.com/rayd360/test-songst'时如何打印出打印矢量
- 在打印机上打印(windows平台32位)
- 流可以用来在打印机上打印