什么"无法将'指针'从'const hand'转换为'hand&'是什么意思?(C++)

4 c++ reference this

我尝试这样做时会发生错误

friend std::ostream& operator<<(std::ostream& os, const hand& obj)
{
    return obj.show(os, obj);
}
Run Code Online (Sandbox Code Playgroud)

hand是我创建的一个类,show是

std::ostream& hand::show(std::ostream& os, const hand& obj)
{
    return os<<obj.display[0]<<obj.display[1]<<obj.display[2]<<obj.display[3]<<obj.display[4];
}
Run Code Online (Sandbox Code Playgroud)

显示声明为char display[6].

有谁知道这个错误意味着什么?

Emp*_*ian 9

你需要制作hand::show(...)一个const方法; 传递obj引用是没有意义的 - 它已经将它作为' this'指针接收.

这应该工作:

class hand {
public:
  std::ostream& show(std::ostream &os) const;
...
};

friend std::ostream& operator<<(std::ostream& os, const hand& obj)
{
    return obj.show(os);
}
Run Code Online (Sandbox Code Playgroud)