Hi,
I'm trying to learn a few Algorithms and function objects
But when i try the following program i get an error which i could not
understand.
Can you please help...
Regards,
Senthil.
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
template <class T> class Print
{
public:
ostream outStream;
public:
Print(ostream ostr):outStream(ostr) { }
void operator ( ) (const T& val)
{
outStream<<val<<endl;
}
};
int main()
{
vector<int> arr;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);
arr.push_back(4);
arr.push_back(5);
arr.push_back(6);
arr.push_back(7);
for_each(arr.begin(),arr.end(),Print<int>(cout)); // << Error occurs
here
return 0;
}
But while compiling i get an error
for_each.cpp: In method `ostream::ostream(const ostream &)':
/ap/gcc/gcc-bala/gcc-2.95.3/lib/gcc-lib/sparc-sun-solaris2.6/2.95.3/../../../../include/g++-3/streambuf.h:128:
`ios::ios(const ios &)' is private
for_each.cpp:30: within this context
for_each.cpp: In function `int main()':
for_each.cpp:30: implicit declaration of function `int for_each(...)'
for_each.cpp:30: warning: cannot pass objects of type `Print<int>'
through `...'