错误 C2062:引用 std::map 时键入意外

Error C2062: type unexpected when referencing std::map

本文关键字:意外 std C2062 引用 错误 map      更新时间:2023-10-16
#include <string>
#include <map>
namespace myNamespace
{
    struct MyStruct
    {
        static std::map<int, std::string> idNameMap;
        // some other static properties
    };
    class MyClass
    {
    private:
        void myMethod() {
            std::map<int, std::string>& myMap = MyStruct::idNameMap; // C2062: type 'int' unexpected
            for (auto& it : myMap)
            {
                // do some stuff with map values
            }
        }
    };
}

我正在尝试引用MyStruct中的静态映射属性,但它产生了此错误。我不确定是否需要更多上下文,但如果需要,请告诉我。

让我们再试一次...

您可能在代码中的某个地方有一个错误终止的定义,就在 int 变量之前,该定义未包含在您的问题中。

即,通过对已发布代码的添加,您将获得完全相同的编译器错误:

#include <string>
#include <map>
double x = 42.0, // ERROR! incorrectly terminated before an 'int' variable
int a = 0;
namespace myNamespace
{
...