473,387 Members | 1,569 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,387 software developers and data experts.

Error with Function object..

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 `...'

Jul 19 '05 #1
4 2254
Senthilvel Samatharman wrote:
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::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 `...'


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):outStream(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

Jul 19 '05 #2
Senthilvel Samatharman wrote:
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 is declared in <algorithm> I believe.
for_each.cpp:30: warning: cannot pass objects of type `Print<int>'
through `...'

Jul 19 '05 #3
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!

"Senthilvel Samatharman" <Sa********************@adcc.alcatel.be> wrote in
message news:3F***************@adcc.alcatel.be...
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 `...'

Jul 19 '05 #4
Dave Theese wrote:
For one thing

<snip>

Please don't top-post. Re-read section 5 of the FAQ for posting
guidelines. You could also find this rule in pretty much any netiquette
reference, including RFC 1855.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Bamberg | last post by:
Hello All, I have been trying without luck to get some information on debugging the Runtime Error R6025 - Pure Virtual Function Call. I am working in C++ and have only one class that is derived...
3
by: Kakaiya | last post by:
Error message: An unhandled exception of type 'System.InvalidCastException' occurred in test.dll Additional information: Specified cast is not valid. Public Class Form1 Inherits...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
0
by: MarkD | last post by:
I have an ASP.NET (VB.NET) application that calls all VB6 COM DLL via Interop. The DLL uses functionality contained in a Custom OCX Control (Also VB6) that in turn contains a standard TreeView...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
1
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.