What does it mean "cannot convert the 'this' pointer from" const hand "to" hand & "mean? (C ++)

An error occurs when I try to do this.

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

where hand is the class I created and 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]; } 

where the display is declared as char display[6] .

Does anyone know what error means?

+4
source share
2 answers

You need to make a method hand::show(...) a const ; and it makes no sense to pass its link obj - it already accepts this as a pointer to ' this '.

This should work:

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

You need the function itself to also be const (note the "const" at the end of the first line):

 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]; } 
+2
source

All Articles