'const hand'에서 'hand &'mean으로 "이 '포인터를 변환 할 수없는 것은 무엇입니까? (c ++)
문제
이 작업을 수행 할 때 오류가 발생합니다
friend std::ostream& operator<<(std::ostream& os, const hand& obj)
{
return obj.show(os, obj);
}
손이 내가 만든 수업 인 곳, 쇼는
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];
}
디스플레이가 선언 된 곳 char display[6]
.
오류가 무엇을 의미하는지 아는 사람이 있습니까?
해결책
당신은 만들어야합니다 hand::show(...)
ㅏ const
방법; 그리고 그것을 전달하는 것은 의미가 없습니다.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);
}
다른 팁
당신은 또한 const이기 위해서도 함수 자체가 필요합니다 (첫 번째 줄의 끝에 "const"를 주목하십시오) :
std::ostream& hand::show(std::ostream& os, const hand& obj) const
{
return os<<obj.display[0]<<obj.display[1]<<obj.display[2]<<obj.display[3]<<obj.display[4];
}
제휴하지 않습니다 StackOverflow