473,387 Members | 1,379 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.

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_inside2' 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::shapename << " " << 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(complex<double> first, complex<double> second):
Shape("Rectangle"), 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::shapename << " " << 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(complex<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::shapename << " " << 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_inside2(Container& c, Rectangle &w){

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

for_each(c.begin(), 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_back(&c);
shapes.push_back(&r);
shapes.push_back(&t);

draw_all_seq_inside2(shapes, r);

return 0;
}

Jul 23 '05 #1
11 2777
fr*******@hotmail.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_inside2' 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**********************@z14g2000cwz.googlegroups .com>,
fr*******@hotmail.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_inside2' 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_window2_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_window2_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_window2_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*******@hotmail.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*******@hotmail.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_window2_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*******@hotmail.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
Thanks everybody i managed to get the program to work without your help
but thanks for the effort. Victor Bazarov if you dont want to help
somebody then please dont write them messages

Jul 23 '05 #11
fr*******@hotmail.com wrote:
Thanks everybody i managed to get the program to work without your help
but thanks for the effort. Victor Bazarov if you dont want to help
somebody then please dont write them messages


Actually, I agree with Victor on this one.

You'll need to read the FAQ for this news-group.

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

In general, Victor is one of the most helpful people you'll know.

Jul 23 '05 #12

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

Similar topics

4
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
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
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...
0
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...
2
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?...
7
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...
6
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
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...
9
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.