错误C2062:引用std :: map

时间:2017-01-23 14:18:42

标签: c++ dictionary

#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中的静态地图属性,但它产生了这个错误。我不确定是否需要更多上下文,但如果是,请告诉我。

1 个答案:

答案 0 :(得分:0)

让我们再试一次......

你的代码中某处可能有一个错误的终止定义,就在你的问题中没有包含的int变量之前。

即。添加到您发布的代码中,您将获得完全相同的编译器错误:

#include <string>
#include <map>

double x = 42.0, // ERROR! incorrectly terminated before an 'int' variable
int a = 0;

namespace myNamespace
{
...