Jump to content
  • Advertisement
Sign in to follow this  
Pendu

Function objects and for_each

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi people, I need some information on function objects particulalry a unary function and for_each. I need to combine the two. This will then have the work the same way as a previus function which took in a container and an objectA, then those objects in the container had a method which takes in objectA. This method was called for each mobject inside the container. Althought its not important to know what the method i'm modeling this unary fucntion on does i just need to know how the hell they work.

Share this post


Link to post
Share on other sites
Advertisement
hi guys this is my code for a functor. I am getting a compile error about the virtual function inside_rectangle. I am not quite sure why?

class UnaryFunction
{
private:
rectangle &win;

public:
UnaryFunction(rectangle &x) : win(x) {}

void operator()(const shape* s) const {
s->inside_rectangle(win);
}

};

Share this post


Link to post
Share on other sites
Oky sorry for the number of posts aleady in this thread by me. I have the follwing functor

class UnaryFunction
{
private:
coord &point1;
coord &point2;
coord &point3;
coord &point4;

public:
UnaryFunction(coord &w, coord &x, coord &y, coord &z) : point1(w), point2(x), point3(y), point4(z) {}

void operator()(shape* s) const {
s->inside_rectangle(point1, point2, point3, point4);
}

};

next i have a function which calls a for_each to the functor.

template <typename Container>
void draw_all_seq_inside2( const Container & c, rectangle & w)
{
coord c1 = w.Get_Coord(0);
coord c2 = w.Get_Coord(1);
coord c3 = w.Get_Coord(2);
coord c4 = w.Get_Coord(3);
for_each(c.first(), c.last(), UnaryFunction(c1, c2, c3, c4);
}

now it doesn't compile and the problem lies in the for_each line. I have not used for_each at all. I have just read up on it from around 3 books. I just have a basic understanding of why you use it and how it works. But not implementing it.

Share this post


Link to post
Share on other sites
We will not be able to guess on our own the decisive information contained in the compiler error message. Could you please mention it?

[EDIT] Now that I look at it, you're missing a closing bracket : ).

Share this post


Link to post
Share on other sites
Oh yes i am damn it lol. Instead of calling the method i decided to test if the for_each worked and it doesn't seem to the error i get is no matching function. I have wrote it in main exactly the same way as i have in the method which also calls it.

Share this post


Link to post
Share on other sites
You might want to give your class a more descriptive name (to me, the name UnaryFunction implies that it's a general interface for unary functors).

Share this post


Link to post
Share on other sites
oky in int main this is what is wrote:-
int main()
{
coord c1(0,0);
coord c2(0,8);
coord c3(8,0);
coord c4(8,8);// rectangle corners

coord cen(4,4);
coord cir(4,6);
circle circ(cen,cir);
vector<shape*> shapes;
shapes.push_back(&circ);
for_each(shapes.first(), shapes.last(), UnaryFunction(c1, c2, c3, c4));
return 0;
}

the UnaryFunction is

class UnaryFunction
{
private:
coord &point1;
coord &point2;
coord &point3;
coord &point4;

public:
UnaryFunction(coord &w, coord &x, coord &y, coord &z) : point1(w), point2(x), point3(y), point4(z) {}

void operator()(shape* s) const {
s->inside_rectangle(point1, point2, point3, point4);
}

};

my error says in int main()
line 736 (which is for_each(shapes.first(), shapes.last(), UnaryFunction(c1, c2, c3, c4));)

'no matching function'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!