Clearly I'm not understanding the difference between "ostream << endl" and "ostream << type". I want a class that behaves exactly like an ostream (in regards to <<) except that it is just a wrapper for two ostreams. I am having problems with the endl, flush, etc.
Code:
#include <ostream>
#include <iostream>
#include <fstream>
using std::ostream;
class dubbstream
{
public:
ostream & s1;
ostream & s2;
explicit dubbstream(ostream & stream1, ostream & stream2)
: s1(stream1), s2(stream2) {}
};
template<typename T>
dubbstream & operator << (dubbstream & dubb, T & arg)
{
dubb.s1 << arg;
dubb.s2 << arg;
return dubb;
}
template<typename T>
dubbstream & operator << (dubbstream & dubb, const T & arg)
{
dubb.s1 << arg;
dubb.s2 << arg;
return dubb;
}
template <class charT, class traits>
dubbstream & operator << (dubbstream & dubb, std::basic_ostream<charT,traits> & arg)
{
dubb.s1 << arg;
dubb.s2 << arg;
return dubb;
}
int main(int argc, char* argv[])
{
using namespace std;
ofstream fout("mer.txt");
dubbstream say(cout, fout);
say << "Hi" << (24 * 4.112) << 'l' << "ala\n"; //okay
say << endl; //error
return 0;
}
Code:
dubbstream.cpp
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2914: 'operator <<' : cannot deduce template argument as function argument is ambiguous
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2914: 'operator <<' : cannot deduce template argument as function argument is ambiguous
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2914: 'operator <<' : cannot deduce template argument as function argument is ambiguous
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::error_code &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1085) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(968) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(958) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const unsigned char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(951) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,signed char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(944) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const signed char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(937) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,_Elem)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(898) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const _Elem *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(851) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(811) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(764) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(726) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const char *)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'dubbstream'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679) : see declaration of 'std::operator <<'
1>c:\users\david\documents\visual studio 2010\projects\dubbstream\dubbstream\dubbstream.cpp(48): error C2676: binary '<<' : 'dubbstream' does not define this operator or a conversion to a type acceptable to the predefined operator
1>
1>Build FAILED.
What is going on with the definition of operator<<(ostream, endl-type-thing) ? I don't have my Stroustrup book on me anymore.
Thanks.