重载<<报错

重载<<报错,第1张

重载<<报错

重载<<报错: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;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5702699.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存