PDA

View Full Version : C++ functor compile problem


rg3
09-02-2005, 12:07 PM
Hello, I have a somehow weird compile error when using a functor in C++ and I don't understand where the problem is. This is the code in question:

http://rafb.net/paste/results/9afr4p15.html

As you see, in the beginning of the file (line 13) I defined a functor to work as an adaptable binary function. It inherits from std::binary_function and, according to the template and the operator(), it receives two arguments of type `Casilla *' and `int', and it returns `void'.

`Casilla' objects have a `remove' method which needs an `int'. In line 44 I try to use this functor to call the `remove' method for each object pointed to from a vector. Note that the "casillas" member is of type `std::vector<Casilla *>', which is exactly what I need. Everything looks fine on my side but, when trying to compile the file. I get this (and only this) error:

http://rafb.net/paste/results/PCPgF854.html

The worst part is that I don't really understand what it's expecting (lines 6 and 7 in the error message). I do understand the only candidate it found, and I know that's what I have.

dark_angel
09-02-2005, 12:30 PM
What the functor term stand for in C++ ?

DudeMiester
09-02-2005, 12:38 PM
The error has to do with you not being const-correct somewhere. Just so you know, C++ doesn't allow temporaries to be directly passed to non-const references. I've ran into this pitfall more then a few times.

I'm thinking change "(Casilla *c, int value)" to "(Casilla *c, const int value)", and "<Casilla *, int, void>" to "<Casilla *, const int, void>". Generally I just mess around with syntatic sugar in these cases until it works.

A functor is basically an address/handle to a function, which usually can have arguments bound to it's parameters.

rg3
09-02-2005, 01:32 PM
If I change that, I get this error message:

http://rafb.net/paste/results/t6IRnS40.html

Just so you know, I changed my strategy and built a unary function instead, to avoid usage of the std::bind2nd function. The current code looks like this and compiles perfectly without any errors:

http://rafb.net/paste/results/35Qygm74.html

The new functor is at line 21 and the line where I use it is number 65. I don't really know what's the problem with std::bind2nd. Whenever I tried to use it with my own adaptable binary functions, it has always crapped on me. In this case my code was so simple that I couldn't stand it and I had to ask.

Still, if somebody could give me more details about the error and things to try, please tell me because I'm really puzzled about it and I won't sleep well until I know what's wrong http://forums.3drealms.com/ubbthreads/images/graemlins/smile.gif.

dark_angel
09-03-2005, 12:49 AM
DudeMiester said:
The error has to do with you not being const-correct somewhere. Just so you know, C++ doesn't allow temporaries to be directly passed to non-const references. I've ran into this pitfall more then a few times.

I'm thinking change "(Casilla *c, int value)" to "(Casilla *c, const int value)", and "<Casilla *, int, void>" to "<Casilla *, const int, void>". Generally I just mess around with syntatic sugar in these cases until it works.

A functor is basically an address/handle to a function, which usually can have arguments bound to it's parameters.



ah! it is somewhat like Delegates in C#!

DudeMiester
09-03-2005, 01:46 AM
Well I've heard the STL implementation of functors is less then great anyways, and you should just use Boost. In fact, much of Boost will be the next STL anyways, so you might as well learn it and get ahead of the curve.

Destroyer
09-06-2005, 11:07 PM
non of those links work.

rg3
09-07-2005, 11:14 AM
I know, the paste site removes them after some days without access, and I no longer have the original code.