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:rintResult), 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: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: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:stream&>, _Tp = std: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
/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:stream&> >::binder2nd(const
If I call a member function with a int parameter, it is OK.
But I want a member function with a ostream& parameter, how?
#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:rintResult), 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: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: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:stream&>, _Tp = std: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:stream&>&, std::basic_ostream<char, std::char_traits<char> >&)'`std::binder2nd said:>::binder2nd(const std::const_mem_fun1_ref_t<void, Ghost,
/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:stream&> >::binder2nd(const
make: *** [test_algorithm] Error 1std::binder2nd said:
If I call a member function with a int parameter, it is OK.
But I want a member function with a ostream& parameter, how?