Reputation: 22346
I've read numerous questions of those with similar problems, but most of the time they boil down to people using function pointers instead of method pointers, or omitting the class scope when creating the pointer instance. However I'm doing neither of those (I think...):
class Test
{
public:
Test() { mFuncPtrs.insert( 10, &Test::Func<int> ); } // Error!
template <class T>
void Func() {}
private:
typedef void ( Test::*FuncPtr )();
std::map<int, FuncPtr> mFuncPtrs;
};
But this gives:
error: no matching function for call to ‘std::map<int, void (Test::*)(), std::less<int>, std::allocator<std::pair<const int, void (Test::*)()> > >::insert(int, <unresolved overloaded function type>)’
But I'm being explicit with the template type, providing the full scope of the method, and Func()
has no overloads! If it makes any difference I'm using g++ v4.1.2.
Upvotes: 2
Views: 592
Reputation: 69967
You are using the insert()
function of std::map
wrongly. There is no overload of insert()
that takes a key and a value as two separate arguments.
Instead, you need to call it on a std::pair
of key and value:
mFuncPtrs.insert(std::make_pair(10, &Test::Func<int>) );
Or, in C++11, you can use uniform initialization syntax for the pair:
mFuncPtrs.insert( { 10 , &Test::Func<int> } );
Simplest of all, though, would be to avoid insert()
altogether and just use the index operator:
mFuncPtrs[10] = &Test::Func<int>;
Even better, given that all of this happens in the constructor, i.e. at intialization time for the map, again in C++11, you can initialize the map with the pair(s) you want:
class Test
{
public:
Test()
: mFuncPtrs { { 10 , &Test::Func<int> } }
{ }
/* ... */
};
Upvotes: 4