Why I can't use for_each to call a member function with a ostream& parameter?

W

waitan

#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <iomanip>
using namespace std;

class Ghost
{
public:
void PrintResult(ostream& os) const{}

friend istream& operator >>(istream& is, Ghost& s);
friend ostream& operator <<(ostream& os, const Ghost& s);
};

istream& operator >>(istream& is, Ghost& s)
{
return is;
}

ostream& operator <<(ostream& os, const Ghost& s)
{
return os;
}

int main(int argc, char* argv[])
{
vector<Ghost> all;
}

for(int i = 0; i < 10; ++i)
{
all.push_back(Ghost());
}

//output to file
ofstream ofile("abc");
if(!ofile)
{
cout << "open file error\n" << endl;
return 1;
}

for_each(all.begin(), all.end(),
bind2nd(mem_fun_ref(&Ghost::printResult), ofile));

ofile.close();

return 0;
}

But I get follow compile error information:

g++ -g -O0 -Wall -o test_algorithm test_algorithm.cpp

/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h:
In instantiation of `std::binder2nd<std::const_mem_fun1_ref_t<void,
Ghost, std::eek:stream&> >':
test_algorithm.cpp:48: instantiated from here

/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h:436:
error: forming reference to reference type `std::eek:stream&'

/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h:
In function `std::binder2nd<_Operation> std::bind2nd(const
_Operation&,
const _Tp&) [with _Operation = std::const_mem_fun1_ref_t<void, Ghost,
std::eek:stream&>, _Tp = std::eek:fstream]':
test_algorithm.cpp:48: instantiated from here

/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h:455:
error: no matching function for call to
`std::binder2nd said:
>::binder2nd(const std::const_mem_fun1_ref_t<void, Ghost,
std::eek:stream&>&, std::basic_ostream<char, std::char_traits<char> >&)'

/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h:429:
note: candidates are: std::binder2nd<std::const_mem_fun1_ref_t<void,
Ghost, std::eek:stream&> >::binder2nd(const
std::binder2nd said:
make: *** [test_algorithm] Error 1


If I call a member function with a int parameter, it is OK.
But I want a member function with a ostream& parameter, how?
 
V

Victor Bazarov

waitan said:
error: forming reference to reference type `std::eek:stream&'

You're trying to form a reference to a reference. Prohibited.
If I call a member function with a int parameter, it is OK.
But I want a member function with a ostream& parameter, how?

I don't remember if there is a way. bind2nd attempts to create
a reference to the type that is the second type of the function
you're trying to bind. Search the archives for "reference to
a reference" and "bind2nd"...

V
 
Z

Zara

On 21 Feb 2006 19:27:33 -0800, "waitan" <[email protected]>
wrote:

If I call a member function with a int parameter, it is OK.
But I want a member function with a ostream& parameter, how?


You may just use a functor created around the ostream You may also use
boost::bind.You may also try to use an stream iterator instead of the
stream. Whichever you feel is easier or fits better with your
programming style.


Zara
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top