简体   繁体   English

&#39;std::string {aka std::basic_string 赋值中的不兼容类型<char> }&#39; 到

[英]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 .注意:数组索引范围是nn-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.

相关问题 std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误无法转换 'std::string* {aka std::basic_string<char> *}' 到 'node*' 在分配</char> - Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:“ operator ==”不匹配(操作数类型为“ Seat”和“ std :: string {aka std :: basic_string <char> }&#39;) - error: no match for 'operator==' (operand types are 'Seat' and 'std::string {aka std::basic_string<char>}') 为什么我收到错误:无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char*&#39; 赋值? - Why am I getting error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in assignment? C ++错误:无法调用&#39;(std :: string {aka std :: basic_string <char> })(std :: string&)&#39; - C++ Error: no match for call to ‘(std::string {aka std::basic_string<char>}) (std::string&)’ 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM