Having trouble figuring out how ostream functors work

This is a discussion on Having trouble figuring out how ostream functors work within the C++ Programming forums, part of the General Programming Boards category; Clearly I'm not understanding the difference between "ostream << endl" and "ostream << type". I want a class that behaves ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891

    Having trouble figuring out how ostream functors work

    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.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    std::endl is actually a function. The idea is that the operator<< that reads it in calls it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    std::endl is a function, not a reference to a stream.

    Replace the last template function with
    Code:
    dubbstream & operator << (dubbstream & dubb, std::ostream & (*arg)(std::ostream &))
    {
    	dubb.s1 << arg;
    	dubb.s2 << arg;
    	return dubb;
    }
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Thanks
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. List of Functors
    By golfinguy4 in forum C++ Programming
    Replies: 13
    Last Post: 09-29-2009, 01:53 PM
  2. for_each and functors
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2009, 07:00 AM
  3. Storing functors?
    By Elysia in forum C++ Programming
    Replies: 18
    Last Post: 11-13-2008, 02:21 AM
  4. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  5. Functors
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2006, 09:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21