'const hand'에서 'hand &'mean으로 "이 '포인터를 변환 할 수없는 것은 무엇입니까? (c ++)

StackOverflow https://stackoverflow.com/questions/928662

  •  06-09-2019
  •  | 
  •  

문제

이 작업을 수행 할 때 오류가 발생합니다

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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top