gpt4 book ai didi

c++ - "cannot convert ' 这个从 'const hand' 到 'hand &' 的指针是什么意思? (C++)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:12:35 25 4
gpt4 key购买 nike

当我尝试这样做时出现错误

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

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];
}

其中 display 声明为 char display[6]

有人知道这个错误是什么意思吗?

最佳答案

你需要让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);
}

关于c++ - "cannot convert ' 这个从 'const hand' 到 'hand &' 的指针是什么意思? (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/928662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com