2
int main()
{

std::string my_string= "16657";
std::map<std::string, std::string[2]> m_msg_int;
std::string arrId[2];
arrId[0] = "ABC";
arrId[1] = "XYZ/CDE";
m_msg_int[my_string] = arrId[2];
std::cout<<"MSGID"<<msgId[0]<<endl;

 }

incompatible types in assignment of ‘std::string {aka std::basic_string}’ to ‘std::map, std::basic_string [2]>::mapped_type {aka std::basic_string [2]}’

  • 2
    Yes, `m_msg_int[my_string]` is an array of two strings, and `arrId[2]` would have been one string if it had existed. You need a different approach, and you need to study some more about arrays in your favourite C++ book. – molbdnilo Dec 21 '19 at 16:04

2 Answers2

4

In line

m_msg_int[my_string] = arrId[2];

you are accessing the array arrId out of bounds because it has size of 2 and reading arrId[2] causes undefined behavior. Even if you wouldn't access out of bounds

m_msg_int[my_string]

is a reference to an array of strings and

arrId[2]

is a string. Probably you are trying to assign

m_msg_int[my_string] = arrId;

but in C++ you can't assign a basic array to a basic array. You should use std::array for this

#include <array>
#include <map>
#include <string>

int main()
{
    std::string my_string= "16657";
    std::map<std::string, std::array<std::string, 2>> m_msg_int;
    std::array<std::string, 2> arrId;
    arrId[0] = "ABC";
    arrId[1] = "XYZ/CDE";
    m_msg_int[my_string] = arrId;
    //std::cout<<"MSGID"<<msgId[0]<<endl;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
-1

You assign a std::string to a array of std::string.

m_msg_int[my_string] = arrId;

Now we assign a array of std::string to our map.

NOTE: Array index range is n to n-1. And you can't do this:

m_msg_int[my_string] = arrId[2];

Because arrId have 2 element, But index 2 means third element.

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32