c++ - Pointer to template method gives <unresolved overloaded function type>) -
i've read numerous questions of similar problems, of time boil down people using function pointers instead of method pointers, or omitting class scope when creating pointer instance. i'm doing neither of (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 gives:
error: no matching function call ‘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 template type, providing full scope of method, , func()
has no overloads! if makes difference i'm using g++ v4.1.2.
you using insert()
function of std::map
wrongly. there no overload of insert()
takes key , value 2 separate arguments.
instead, need call on std::pair
of key , value:
mfuncptrs.insert(std::make_pair(10, &test::func<int>) );
or, in c++11, can use uniform initialization syntax pair:
mfuncptrs.insert( { 10 , &test::func<int> } );
simplest of all, though, avoid insert()
altogether , use index operator:
mfuncptrs[10] = &test::func<int>;
even better, given of happens in constructor, i.e. @ intialization time map, again in c++11, can initialize map pair(s) want:
class test { public: test() : mfuncptrs { { 10 , &test::func<int> } } { } /* ... */ };
Comments
Post a Comment