473,890 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with unary function for_each

hello people,
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.

i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called 'draw_all_seq_i nside2' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).

#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <string>
#include <complex>
#include <functional>
#include <algorithm>

using namespace std;

class Shape {
protected:
string shapename;
public:
Shape(string name): shapename(name) {};
virtual void draw() const = 0;

virtual void inside_window2( complex<double> &bl, complex<double>
&tr) const = 0;
};

class Circle : public Shape {
public:
const double x1;
const double y1;
const double radius;

Circle(complex< double> first, double rad): Shape("Circle") ,
x1(first.real() ), y1(first.imag() ), radius(rad) {}

void draw() const {
complex<double> first(x1, y1);
cout << "<Shape::" << Shape::shapenam e << " " << first << " "
<< radius
<< " >\n";
}

void inside_window2( complex<double> &bl, complex<double> &tr) const
{
if((x1 - radius) >= bl.real() && (y1 - radius) >= bl.imag() &&
(x1 + radius) <= tr.real() && (y1 + radius) <= tr.imag()){
draw();
}
}
};

class Rectangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;

Rectangle(compl ex<double> first, complex<double> second):
Shape("Rectangl e"), x1(first.real() ), y1(first.imag() ),
x2(second.real( )), y2(second.imag( )) {}

void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
cout << "<Shape::" << Shape::shapenam e << " " << first << " "
<< second
<< " >\n";
}

void inside_window2( complex<double> &bl, complex<double> &tr) const
{
if(bl.real() <= x1 && bl.imag() <= y1 && tr.real() >= x2 &&
tr.imag() >= y2){
draw();
}

}

};

class Triangle : public Shape {
public:
const double x1;
const double y1;
const double x2;
const double y2;
const double x3;
const double y3;

Triangle(comple x<double> first, complex<double> second,
complex<double> third):
Shape("Triangle "), x1(first.real() ), y1(first.imag() ),
x2(second.real( )),
y2(second.imag( )), x3(third.real() ), y3(third.imag() ) {}

void draw() const {
complex<double> first(x1, y1);
complex<double> second(x2, y2);
complex<double> third(x3, y3);
cout << "<Shape::" << Shape::shapenam e << " " << first << " "
<< second
<< " " << third << " >\n";
}

void inside_window2( complex<double> &bl, complex<double> &tr) const
{
double wx1 = bl.real();
double wy1 = bl.imag();
double wx2 = tr.real();
double wy2 = tr.imag();
if(wx1 <= x1 && wx1 <= x2 && wx1 <= x3 && wy1 <= y1 && wy1 <=
y2 && wy1 <= y3 &&
wx2 >= x1 && wx2 >= x2 && wx2 >= x3 && wy2 >= y1 && wy2 >=
y2 && wy2 >= y3){
draw();
}
}

};
template <typename Container>
void draw_all_seq_in side2(Container & c, Rectangle &w){

complex<double> first(w.x1, w.y1);
complex<double> second(w.x2, w.y2);

for_each(c.begi n(), c.end(), mem_fun(&Shape: :inside_window2 (first,
second)));

}

int main() {
vector<Shape *> shapes;
complex<double> first(0, 0);
complex<double> secondr(1, 1);
complex<double> secondt(0, 1);
complex<double> third(1, 0);

Circle c = Circle(first, 1);
Rectangle r = Rectangle(first , secondr);
Triangle t = Triangle(first, secondt, third);
shapes.push_bac k(&c);
shapes.push_bac k(&r);
shapes.push_bac k(&t);

draw_all_seq_in side2(shapes, r);

return 0;
}

Jul 23 '05 #1
11 2820
fr*******@hotma il.com wrote:
[..]
i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called 'draw_all_seq_i nside2' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).
[..]


Please RTFM. std::for_each is can only call a function with one argument
and that argument should be the result of dereferencing the iterator (or
one convertible from the result of dereferencing the iterator). IOW, one
possible implementation of 'std::for_each' is this:

template<class I, class F>
F my_for_each(I i1, I i2, F f)
{
while (i1 != i2)
f(*i1++);
return f;
}

If you read this and try to understand what it does, you will see that you
need to supply a _function pointer_ or a _functor_ as the third argument
of 'for_each'. 'mem_fun' is an adapter. It takes a _no-argument_ member
or a _single-argument_ member function and makes a one-argument functor
or a two-argument functor out of it. Yours is a _two-argument_ member
function (which really has three arguments, the first is the hidden object
pointer or reference which inside becomes 'this'). If you want to use
'mem_fun' with it, you have to also use 'bind2nd' with it to pass the
second argument of your member function. That makes the example more
complex than you're ready to tackle. Study more about templates and about
'bind1st' and 'bind2nd' binders and 'mem_fun*' adapters.

If somebody writes it for you, I bet you're not really going to learn much
about those mechanisms. If you want to learn, you have to do it yourself.

There are plenty of examples of using 'bind1st' and 'bind2nd' and also of
'mem_fun' in the archives. I could write another one, but I don't really
see the point.

V
Jul 23 '05 #2
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
fr*******@hotma il.com wrote:
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.

i have written this abtract class shape and its derived class circle,
rectangle and triangle. i have also written this method for each shape
object called 'draw_all_seq_i nside2' which takes two values. i want to
call this method using for_each statement in another method but it
doesnt seem to work. this is a trim version of the code. i need to do
this using for_each statement as it is a coursework. please have a look
and get back to me (sorry for the lengthy code but i trimmed it as much
as possible).


I trimmed your code a bit more:

class Shape {
public:
virtual void inside_window2( complex<double> &bl,
complex<double> &tr) const = 0;
};

void foo( vector<Shape*> c,
complex<double> x, complex<double> y )
{
for_each( c.begin(), c.end(), /* something that will call
inside_window2 on each shape with the arguments x and y */ );
}

The simplest way to do this sort of thing is to put the body of the loop
in a functor then create one and pass it to the for_each function...

So, the inside of the loop looks like this:

void inside_loop( const Shape* s, complex<double> x, complex<double> y )
{
s->inside_window_ 2( x, y );
}

but we want to provide the 'x' and 'y' at a different time than the 's'.
That requires two steps, and the 'x' and 'y' have to be stored...

struct call_inside_win dow2_with {
complex<double> x, y;
foo( complex<double> a, complex<double> b ): x(a), y(b) { }
void operator()( const Shape* s ) const {
s->inside_window2 ( x, y );
}
};

for_each( c.begin(), c.end(), call_inside_win dow2_with( x, y ) );
Jul 23 '05 #3
where would i put the following piece of code for the for_each to work.
i cant figure it out. please use the original file as a template of
give an example.

struct call_inside_win dow2_with {
complex<double> x, y;
foo( complex<double> a, complex<double> b ): x(a), y(b) { }
void operator()( const Shape* s ) const {
s->inside_window2 ( x, y );
}

};

Jul 23 '05 #4
fr*******@hotma il.com wrote:
where would i put the following piece of code for the for_each to work.
i cant figure it out. please use the original file as a template of
give an example.


Whose coursework is it?
Jul 23 '05 #5
fr*******@hotma il.com wrote:

where would i put the following piece of code for the for_each to work.
i cant figure it out. please use the original file as a template of
give an example.

struct call_inside_win dow2_with {
complex<double> x, y;
foo( complex<double> a, complex<double> b ): x(a), y(b) { }
void operator()( const Shape* s ) const {
s->inside_window2 ( x, y );
}

};


look at it closely.
It is just a declaration of a structure. If you prefer, you can
replace the 'struct' with 'class' and add the 'public:' keyword
as needed. Then it would be just an ordinary class declaration,
so you may put it wherever such a thing is valid
(of course, you need to put it somewhere before it is used in the actual
for_each statement).

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #6
i really appreciate your help but there is no time to do indept
research, i came to the forum because i have run out of time and i am
know desperate as i have to hand in the work on wednesday, so if you
can help me then it would be deply appreciated

Jul 23 '05 #7
fr*******@hotma il.com wrote:
i really appreciate your help but there is no time to do indept
research, i came to the forum because i have run out of time and i am
know desperate as i have to hand in the work on wednesday, so if you
can help me then it would be deply appreciated


You came too late. If whatever has been suggested can't help you at this
point, I recommend you ask for an extension or talk to your instructor or
teacher about handing in incomplete work. The policy of this newsgroup
is not to do homework for anybody. I am sure I don't speak for everybody
here, I am just stating facts.
Jul 23 '05 #8
they complex<double> objects which the inside_window2 method accepts is
not available when the Shape objects are initialized. this is why i am
having difficulty placing it somewhere in my code. i only picked up c++
a month ago as a new module (java programmer) so i am not very
confident with it which is why i really need your help.

Jul 23 '05 #9
i have done my homework thank you. i wrote the class and the other
methods, i am asking for you help because this is a more difficult part
of the coursework, i am not asking you to do it for me, i am just
asking for something to work with. i am new to c++ (only picked it up
this term) and we havent studied anything on unaryfunction.

Jul 23 '05 #10

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

Similar topics

4
2275
by: Senthilvel Samatharman | last post by:
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.
3
1505
by: Griff | last post by:
#include <iostream> using namespace std; #include <vector> #include <string> #include <fstream> #include <algorithm> template<class C>void PrintAll(C&v) {
3
1644
by: Tony Johansson | last post by:
Hello! I have some problem with STL. I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function Now to my problem.
0
1478
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function. In main you put in nodes in the STL list and you can display the STL list and other things such as erase some value from...
2
2274
by: IndyStef | last post by:
I am trying to use boost's bind on a member function, on the VC8 compiler. After using several different attempts, I could not get it to work. Does anybody know what is wrong with the code below? The function that doesn't compile is foo::DoTheStuff. All three variations of the for-each loop won't build.
7
1972
by: nabeel.girgis | last post by:
I am passing a vector by reference into a function and I am trying to use a pointer in that function. I get an error saying : '=' : cannot convert from 'std::vector<_Ty> *' to 'int *' when I try to initialize the pointer to point to the vector. This is my first time using pass by reference into a function while trying to declare a pointer in the same function. My code is given below.
6
4040
by: Belebele | last post by:
Suppose I want to use some object's member function as the action passed to a for_each call: class A { ... public: void foo(int ); }; A a;
6
1694
by: learning | last post by:
I am trying to learn STL but got stuck in for_each(). What I intend to do in the following is to make a list with each element as a string. add the string elements to the list until it has 10 elements. each element is the same 1234567890. then I want to use for_each to iterate the list, and for each to call apply() whihc is create a filen with name the same as string al; and within each file, it contains the content of the list...
9
2111
by: want.to.be.professer | last post by:
We know that alomost every algorithm function, such as for_each, find_if, use funcional as well as function pointer. But when I want to use another class's member function, how could I do? See example: class TestPrint { public:
0
9979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9825
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9638
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6052
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.