0

I want to use reference in std::map

typedef const std::function<void(const cocos2d::Ref*)>& callBack;

std::map<const std::string&, callBack> m_mapListener

and my error message is:

error C2535: 'const std::function<void (const cocos2d::Ref *)> &std::map<const std::string &,callBack,
std::less<_Kty>,std::allocator<std::pair<_Kty,_Ty>>>::operator [](const std::basic_string
<char,std::char_traits<char>,std::allocator<char>> &)' : 
member function already defined or declared 
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\map

How could I fix it?

1
  • 5
    This has nothing to do with the std::function. Next time perform some experimentation, such as taking away the std::function and seeing whether your problem remains (which it does). Also, present proper testcases in your questions: here your error message complains about operator[], but you don't ever show us actually using operator[]. It's pure chance that we can fix the problem for you regardless. Commented Jan 22, 2016 at 10:16

1 Answer 1

3

Define your map as:

std::map<std::string, callBack> m_mapListener;

STL containers don't support references because they require that their element type meets the requirements of Erasable, in which case references don't.

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

Comments

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.