我正在为我的应用程序编写一个输出管理器类,尽管我的类在linux下的g++上工作,但它不会在MSVC++2010下编译。
这是我的程序的SSCCE版本:
#include <iostream>
#include <malloc.h>
#include <sstream>
#include <fstream>
template<class cT, class traits=std::char_traits<cT> >
class BasicE3OutStream: public std::basic_ostream<cT, traits> {
public:
BasicE3OutStream() :
std::basic_ios<cT, traits>(), std::basic_ostream<cT, traits>(0), mBuffer() {
resetModifiers();
mLineBuffer=(std::stringstream*) malloc(sizeof(std::stringstream));
mLineBuffer=new (mLineBuffer) std::stringstream();
}
BasicE3OutStream(const BasicE3OutStream<cT,traits> &) {
}
virtual ~BasicE3OutStream() {
delete mLineBuffer;
mLineBuffer=NULL;
}
void resetModifiers() {
mDebug=false;
mMemory=false;
}
void open(const char* fileName) {
mBuffer.open(fileName, std::stringstream::out);
}
void close() {
mBuffer.close();
}
template<class T>
BasicE3OutStream& operator<<(T val) {
(*mLineBuffer)<<val;
return (*this);
}
BasicE3OutStream& operator<<(char* val) {
(*mLineBuffer)<<val;
return (*this);
}
BasicE3OutStream& operator<<(std::string val) {
(*mLineBuffer)<<val;
return (*this);
}
void addLineFeed() {
std::string modifier;
modifier="";
if (mDebug)
modifier+="[DEBUG] ";
else if (mMemory)
modifier+="[MEMORY] ";
mBuffer<<modifier<<mLineBuffer->str()<<"n";
mLineBuffer->~basic_stringstream();
mLineBuffer=new (mLineBuffer) std::stringstream();
resetModifiers();
}
void flush() {
mBuffer.flush();
resetModifiers();
}
void setFlag(int f) {
if (f==0)
mDebug=true;
else if (f==1)
mMemory=true;
}
public:
std::basic_ofstream<cT, traits> mBuffer;
std::stringstream* mLineBuffer;
bool mDebug, mMemory;
};
template<class cT, class traits, class T>
BasicE3OutStream<cT, traits>& operator<<(BasicE3OutStream<char, traits>& str , T val) {
str.operator <<(val);
return (str);
}
typedef BasicE3OutStream<char> E3OutStream;
template<class charT, class traits>
BasicE3OutStream<charT, traits>& endl(BasicE3OutStream<charT, traits>& os) {
os.addLineFeed();
return (os);
}
template<class charT, class traits>
BasicE3OutStream<charT, traits>& flush(BasicE3OutStream<charT, traits>& os) {
os.flush();
return (os);
}
template<class charT, class traits>
BasicE3OutStream<charT, traits>& debug(BasicE3OutStream<charT, traits>& os) {
os.setFlag(0);
return (os);
}
template<class charT, class traits>
BasicE3OutStream<charT, traits>& memory(BasicE3OutStream<charT, traits>& os) {
os.setFlag(1);
return (os);
}
/**
* Io manipulator, allows to use endl and other modificators
*/
template<class charT, class traits> BasicE3OutStream<charT, traits>& operator<<(
BasicE3OutStream<charT, traits> &s
, BasicE3OutStream<charT, traits>& (*iomanip)(BasicE3OutStream<charT, traits>&)) {
return (iomanip(s));
}
int main() {
E3OutStream s;
s.open("output.txt");
s<<debug<<"Debug info"<<endl;
s<<flush;
s<<memory<<"Memory info"<<endl;
s<<flush;
s.close();
}
它在g++中运行良好,但在MSVC上我得到了一个编译器错误:
1>------ Build started: Project: sscce, Configuration: Debug Win32 ------
1> main.cpp
1>c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(129): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(811): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(937): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(944): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(951): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(958): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(1085): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(43): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1> with
1> [
1> cT=char
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(48): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1> with
1> [
1> cT=char
1> ]
1> while trying to match the argument list '(E3OutStream, overloaded-function)'
1>c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(130): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(811): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(937): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(944): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(951): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(958): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(1085): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(43): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1> with
1> [
1> cT=char
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(48): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1> with
1> [
1> cT=char
1> ]
1> while trying to match the argument list '(E3OutStream, overloaded-function)'
1>c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(131): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(811): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(937): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(944): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(951): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(958): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(1085): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(43): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1> with
1> [
1> cT=char
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(48): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1> with
1> [
1> cT=char
1> ]
1> while trying to match the argument list '(E3OutStream, overloaded-function)'
1>c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(132): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(726): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(764): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(811): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(937): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(944): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(951): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(958): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(1085): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' [found using argument-dependent lookup]
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(43): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(char *)'
1> with
1> [
1> cT=char
1> ]
1> c:usersguillaumedocumentsvisual studio 2010projectsssccessccemain.cpp(48): or 'BasicE3OutStream<cT> &BasicE3OutStream<cT>::operator <<(std::string)'
1> with
1> [
1> cT=char
1> ]
1> while trying to match the argument list '(E3OutStream, overloaded-function)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
任何帮助都将不胜感激,Guillaume
[编辑]
多亏了这个建议,我才意识到在这种情况下扩展iostream是没有用的,因为我已经有了一个ofstream作为成员变量。由于我对std库的内部工作还不够了解,我让我的类独立,它在MSVC和g++上都能工作:
#include <iostream>
#include <malloc.h>
#include <sstream>
#include <fstream>
class BasicE3OutStream {
public:
BasicE3OutStream() :
mBuffer() {
resetModifiers();
mLineBuffer=(std::stringstream*) malloc(sizeof(std::stringstream));
mLineBuffer=new (mLineBuffer) std::stringstream();
}
BasicE3OutStream(const BasicE3OutStream &) {
}
virtual ~BasicE3OutStream() {
delete mLineBuffer;
mLineBuffer=NULL;
}
void resetModifiers() {
mDebug=false;
mMemory=false;
}
void open(const char* fileName) {
mBuffer.open(fileName, std::stringstream::out);
}
void close() {
mBuffer.close();
}
template<class T>
BasicE3OutStream& operator<<(T val) {
(*mLineBuffer)<<val;
return (*this);
}
void addLineFeed() {
std::string modifier;
modifier="";
if (mDebug)
modifier+="[DEBUG] ";
else if (mMemory)
modifier+="[MEMORY] ";
mBuffer<<modifier<<mLineBuffer->str()<<"n";
mLineBuffer->str("");
resetModifiers();
}
void flush() {
mBuffer.flush();
resetModifiers();
}
void setFlag(int f) {
if (f==0)
mDebug=true;
else if (f==1)
mMemory=true;
}
public:
std::ofstream mBuffer;
std::stringstream* mLineBuffer;
bool mDebug, mMemory;
};
BasicE3OutStream& endl(BasicE3OutStream& os) {
os.addLineFeed();
return (os);
}
BasicE3OutStream& flush(BasicE3OutStream& os) {
os.flush();
return (os);
}
BasicE3OutStream& debug(BasicE3OutStream& os) {
os.setFlag(0);
return (os);
}
BasicE3OutStream& memory(BasicE3OutStream& os) {
os.setFlag(1);
return (os);
}
/**
* Io manipulator, allows to use endl and other modificators
*/
BasicE3OutStream& operator<<(
BasicE3OutStream &s
, BasicE3OutStream& (*iomanip)(BasicE3OutStream&)) {
return (iomanip(s));
}
typedef BasicE3OutStream E3OutStream;
int main() {
E3OutStream s;
s.open("output.txt");
s<<debug<<"Debug info"<<endl;
s<<flush;
s<<memory<<"Memory info"<<endl;
s<<flush;
s.close();
}
问题的核心是向流发送函数指针,GCC似乎知道如何处理,但MSVC++不知道。不管怎样,我怀疑您是否希望保存debug
/endl
/flush
/memory
函数的地址。
下一步的解决方案是使对象而不是显示的函数。
然而,这段代码有很多错误。也就是说,已经存在endl
和flush
对象,您应该使用它们。此外,每个BasicE3OutStream
都有三个底层流,其中一个是从中继承的,所有函数都在其上工作(包括endl
等,我认为这就是您替换它们的原因),以及mBuffer
和mLineBuffer
(您正在做的事情不需要这两个成员)。(另一个旁注,用mLineBuffer.str("");
重置字符串流,而不是放置新的)
执行所需操作的简单方法是重载basic_ostream
的每个operator<<
成员,并且只要有换行符,就可以插入[DEBUG]
或[MEMORY]
作为相应的前缀,不需要缓冲。(我已经做到了,这并不难)
可能有一种right的方法可以做到这一点,即使用具有不同字符特征的流,或者重载某些虚拟函数,或者替换缓冲区,但流很复杂,我不知道这种right方法。