2

I'm having trouble with inserting some value_pairs into a map. Here's the basic idea.

// private
typedef Foo* (*Bar)( const std::string &x, int y );
typedef std::map<std::string, Bar> myMap;

template<class T>
Foo* DoThing( const std::string &x, int y ) {
  return new T( x, y );
}

myMap m_map;

// some map insertion code
m_map.insert( myMap::value_type( "blah", &DoThing<SomeType> ));
m_map.insert( myMap::value_type( "blech", &DoThing<OtherType> ));

This would give a compiler error saying no matching function call to std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Foo* (*)(const std::string&, int)>::pair(const char [5], <unresolved overloaded function type>) Not sure what I'm doing wrong syntactically, or why I'm getting the unresolved overloaded function type. My best guess is that it doesn't know that DoThing returns a Foo*.

Any help appreciated, thanks.

6
  • I'm using g++. Hmm.. Made the template function a bit more specific. Commented Jun 26, 2010 at 6:16
  • 2
    Just to be sure, DoThing<T> is a global function and not a member of any class, right? If it's inside a class, it needs to be a static member function. Commented Jun 26, 2010 at 6:24
  • @Ben Doh! That was the problem! Thanks! Commented Jun 26, 2010 at 6:41
  • 1
    @Ben, write that as a short answer and you will get credit for it and @ggg will be able to accept an answer. Commented Jun 26, 2010 at 9:35
  • @David: I had a slight advantage from answering (fixing the typedef) on his previous question, in which the out-of-line definition of DoThing was provided, and it was clearly a class member (but without the declaration there was still no indication of whether or not it was static). Commented Jun 26, 2010 at 14:35

3 Answers 3

5

Comment converted to answer:

Just to be sure, DoThing<T> is a global function and not a member of any class, right? If it's inside a class, it needs to be a static member function.

Also, if you take the address of a member function, some compilers insist on qualifying it with the class name, e.g. &MyClass::DoThing<T>. Again, if it is a static member function, you get an ordinary function pointer. If a non-static member function, you get a pointer-to-member and the this pointer must be supplied at the call site.

Sign up to request clarification or add additional context in comments.

Comments

1

You forgot a ',' before the second &.

Comments

1

My guess is that it doesn't know how to convert the "blah" and "blech" to std::strings. If you construct these explicitly I think it should work.

1 Comment

That could certainly have been one of the problems, but didn't solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.