A
aaragon
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:
template <class P>
class Q {
typedef 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) {
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
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.
Does anyone know how to do this? I appreciate any suggestions.
a²
problem in order to avoid writing some extra code:
I have a template class that has a member function called map:
template <class P>
class Q {
typedef 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) {
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
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.
Does anyone know how to do this? I appreciate any suggestions.
a²