如何打印职位

How to print the position

本文关键字:打印 何打印      更新时间:2023-10-16

由于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_stry_str是局部变量,而在于赋值没有达到预期的效果。也就是说,不能将int转换为string

您可以使用_itoa()int转换为char*stringstream,如Michael的回答所示。