今天遇到一個非常難以排查的BUG,谷歌度娘都問過了依舊無解,最后自己重新嘗試之后找到解決方案:
先看一下報錯信息:
1>.\lenz.cpp(2197) error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 1> D:\Program Files\VC\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const char *)' 1> with 1> [ 1> _Elem=wchar_t, 1> _Traits=std::char_traits<wchar_t> 1> ] 1> D:\Program Files\VC\include\ostream(697): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,char)' 1> with 1> [ 1> _Elem=wchar_t, 1> _Traits=std::char_traits<wchar_t> 1> ]
......//一行出錯后就會有一堆這樣的With[]....出現,非常煩
網上搜了一下,很多人說是因為沒有#include <string>的緣故,但是反復確認后發現我有做到。百思不得其解,還一度以為是不同文件的頭文件重復引用導致<string>失效,但是這條說不通!后面冷靜了之后,雙擊錯誤信息查看源碼,發現基本上這些錯誤含有兩個特征:
1.都是LOG4CPLUS_DEBUG等調試函數
2.參數都有string類型
然后我單獨寫了一小段程序驗證了一下,好家伙,真是這樣子。原來,LOG4CPLUS_DEBUG函數不能直接打印string對象,只支持其轉換形式.c_str()。
解決方法:把所有的LOG4CPLUS_DEBUG函數中的string加個c_str()