aaragon wrote:
Hi everybody, I was wondering if there is an easy solution to this
problem in order to avoid writing some extra code:
I have a template class that has a member function called map:
DON'T have a name that is already used in standard library as your
identifier. I hardly can go on with your code.
template <class P>
class Q {
typedef P::R RType;
dependent type,
typedef typename P::R RType;
...
RType* map(P*, P*, int) {
... // do some stuff
}
};
Now, in another function contained in class Q, I need to sort a vector
and for that I use a functor:
template <class P>
class SortFunctor {
typedef typename P::R RType;
P* p1_;
P* p2_;
public:
SortFunctor(P* p1, P* p2)
: p1_(p1), p2_(p2) {}
bool operator()(size_t left, size_t right) {
return (map(left) < map(right));
}
RType* map(index) {
RType* map(size_t index) {
if (index < p1_->size())
return p1_->rtype(index);
else
return p2_->rtype(index-p1_->size());
}
};
So, this works perfectly, but I write the same function map in both
Already works? I think it does not even compile
the functor and in the template class Q. Now, I thought that it might
be cool to use a function to a pointer, so I added some extra stuff to
the functor so it looks like this (and of course I removed the
function map()):
template <class P>
class SortFunctor {
typedef typename P::R RType;
P* p1_;
P* p2_;
Rtype* (*map)(P*,P*,int);
public:
SortFunctor(P* p1, P* p2, RType* fp)
: p1_(p1), p2_(p2) { map = fp; }
...
};
and I have the following error message:
error: ISO C++ forbids taking the address of an unqualified or
parenthesized non-static member function to form a pointer to member
function.
On the above, map is pointer to function, which is different from
pointer to member function.
I guess you write something like: &Klass::Fun, this is a pointer to
member function, its type is: ResultType (KlassType::*)(Arg1Type, Arg2Type);
If you have problem in pointer to member function, try to check it out
first.
Does anyone know how to do this? I appreciate any suggestions.
a?
problem. The function is not called map but decodeIndividual (but it
post). But still, what I'm asking is if what I'm trying to accomplish
is feasible or not. I read on the FAQ Lite and I found out that it is
very tricky to use pointers to member functions. They give a solution