By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

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

P: n/a
#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::ostream&> >':
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::ostream&'

/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::ostream&>, _Tp = std::ofstream]':
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<std::const_mem_fun1_ref_t<void, Ghost, std::ostream&>
::binder2nd(const std::const_mem_fun1_ref_t<void, Ghost, std::ostream&>&, 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::ostream&> >::binder2nd(const
std::binder2nd<std::const_mem_fun1_ref_t<void, Ghost, std::ostream&>&)

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?

Feb 22 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
waitan wrote:
error: forming reference to reference type `std::ostream&'
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
--
Please remove capital As from my address when replying by mail
Feb 22 '06 #2

P: n/a
On 21 Feb 2006 19:27:33 -0800, "waitan" <wa**************@sohu.com>
wrote:

<snip>

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
Feb 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.