基本级别的映射,"this const expression has type "const char*" 而不是所需的 "unsigned int" 类型

Maps at a fundamental level, "this const expression has type "const char*" instead of the required "unsigned int" type

所以我已经在谷歌上搜索了一段时间,在这里搜索没有找到任何结果,所以希望这不是重复的

我在为映射中的字符串赋值时遇到了一些问题,但发现了一个更有趣的问题,因为我可以直接复制粘贴在 map::map 的 C++ 参考中找到的示例,它仍然出现在 Visual Studio 的智能感知错误报告中有同样的错误。在下方找到:

#include <map>
#include <iostream>

std::map<char,int> first;

first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;

这是在主 .cpp 加载的头文件中,所以它不在函数中,我还复制粘贴的 fncomp 和 classcomp,以防它是一个被覆盖的内置函数(我绝对没有想法到此为止),不出所料,事实并非如此

是我的 Visual Studio 版本坏了还是怎么的?我看不出我可能错过了什么。

该错误消息毫无意义,但这可能只是因为您在随机空 space.[=10= 中编写这些表达式语句,从而混淆了您的 IDE。 ]

赋值必须在函数中进行。

只有声明可以存在于名称space 范围内。