I have the friends in a class definition obviously and to make it clearer, have removed the implementation code etc...
class Ref { public: Ref(char* s, size_t l) : _s(s), _l(l) {} private: char* _s; size_t _l; }; class foo { public: friend stringstream& operator>>(std::stringstream& s, uint8_t& v) { return s; } friend stringstream& operator>>(std::stringstream& s, const Ref& r) { return s; } private: std::stringstream ss; void example(void) { char b[256]; Ref r(b, sizeof(b)); uint8_t i; ss >> i >> r; <------ Why do I get an error here? ss >> r >> i; <------ But this one seems to compile fine. } };
l(_l)to_l(l), removing thefriendkeywords (since they can't appear outside a class definition) and changingchar* b[256]tochar b[256]and sticking the relevant code in amain(), this compiled just fine for me.ss >> ishould be of typestd::istream, and there's no>>foristream >> Ref.operator>>foruint8_tandstd::stringstreamto return astd::stringstream &and the compiler found that one before the one innamespace stdin<sstream>. After his edit, however, now this does not compile because this same overload is now inclass foothe overload is never considered and the expected one in<sstream>is found through ADL.