473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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

#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
2 2919
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
3
by: xuatla | last post by:
Hi, I have a problem about using a class member function as a parameter in another function. What I tried to implement is something like described below: class A { public:
12
by: Huskier | last post by:
Hi all: I want to pass a class-member function to pthread_create, and my code is in the following, but I don't know how to pass myobj.thread_function to pthread_create function. #include...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
1
by: NorrYtt | last post by:
I am having a problem with passing a function as a parameter to my DLL so it can call it back with updates. The DLL is written in LabWindows CVI with some #ifdef 'C'-style wrappings. The...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
2
by: Azdo | last post by:
Hi, is it possible to pass a member function pointer to other member function of the same class in order to get different behaviours? Something like class A { int m(int d) /* First way */
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.