I want to create a map ,
std::map <MESSAGE_CATEGORY, const std::string> m_mapResponseDesc;
I am using operator[]
to append a value in the map:
m_mapResponseDesc[STATUS_LIMIT] = "Limit has been exceeded";
STATUS_LIMIT
is of type enum
.
I am getting error:
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
Please point out what mistake I have done. I am not getting any clue.
Since operator[]
returns a reference (to a const std::string
) you will need to use the insert()
method instead.
#include <map>
#include <string>
using namespace std;
int main()
{
std::map<int, const std::string> m;
m.insert(std::make_pair(1, "Hello"));
return 0;
}
Update for C++11:
You can do this even easier now:
std::map<int, const std::string> status = {
{200, "OK"},
{404, "Not Found"}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With