[英]incompatible types in assignment of 'std::string {aka std::basic_string<char>}' to
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]}' 'std::string {aka std::basic_string}' 到 'std::map, std::basic_string [2]>::mapped_type {aka std::basic_string [2]}' 赋值中的类型不兼容
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.您正在访问数组arrId
越界,因为它的大小为 2 并且读取arrId[2]
会导致未定义的行为。 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.但是在 C++ 中,您不能将基本数组分配给基本数组。 You should use std::array for this您应该为此使用 std::array
#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;
}
You assign a std::string
to a array of std::string
.您指定std::string
到一个数组std::string
。
m_msg_int[my_string] = arrId;
Now we assign a array of std::string
to our map.现在我们为我们的地图分配一个std::string
数组。
NOTE : Array index range is n
to n-1
.注意:数组索引范围是n
到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.因为arrId
有 2 个元素,但索引2
表示第三个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.