Hello to everyone,
I wonder if and how it is possible to define a function object inside a classes member function to use it directly with, for example, the std::transform function.
I know the example is a bit stupid, it's just to show the problem I'm confronted with.
File "example.h"
class Example {
public:
//.. constructor and destructor stuff
std::string toString() const; //Converts 'mVal' to a std::string
private:
std::vector<int> mVal; //Only one digit numbers are allowed ([0-9])
}
File "example.cpp"
std::string Example::toString() const
{
//The functor which should be used in std::transform
struct {
char operator()(const int number) {
char c;
//"Convert" 'number' to a char
return c;
};
} functor;
//Transform the integers to char
std::string str(mVal.size(), '0'); //Allocate enough space
std::transform(mVal.begin(), mVal.end(), str.begin(), functor);
return str;
};//toString()
Ever since I tried to implement a function object directly inside a member function like in "example.cpp", the code doesn't get compiled. The error message I get is:
error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Example::toString() const::<anonymous struct>&)’
So I think the problem comes up when using the struct "functor" in std::transform. Can someone tell me what the problem is?
Using:
gcc-4.2 compiler under Ubuntu Linux.
Thanks in advance,
René.