Error with Function object..

G

Gianni Mariani

Senthilvel said:
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.

..... sample code removed
But while compiling i get an error

for_each.cpp: In method `ostream::eek:stream(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 `...'

Here is better formatting ...

#include <iostream>
#include <functional>
#include <vector>

using namespace std;

template <class T>
class Print
{
public:
ostream & outStream;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can't copy outStream so we make it a reference

public:
Print(ostream & ostr):eek:utStream(ostr) { }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Similarly here.


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));
return 0;
}


This compled and ran with gcc 3.3.1
 
N

Noah Roberts

Senthilvel said:
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):eek:utStream(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::eek:stream(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(...)'
 
D

Dave Theese

For one thing, you need to #include <algorithm> for for_each() unless it's
somehow getting indirectly included. Not sure if this will fix everything,
but it's a good place to start!
 
S

Senthilvel Samatharman

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):eek:utStream(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::eek:stream(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 `...'
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top