重载<<报错:error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]’ is protected
具体可以看:https://coderedirect.com/questions/608505/about-stdostream-constructor
#include#include using namespace std; template std::ostream& operator << (std::ostream& strm, const std::pair & p) { return strm << "[" << p.first << ", " << p.second << "]"; } int main() { // error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits ]’ is protected // std::ostream strm = std::cout; std::ostream& strm = std::cout; //正确 std::pair p{1, 2}, p2{3, 4}; //std::cout << p << p2; //或者采用 strm << p << p2; return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)