使用“auto”会产生错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int

Using `auto` yields error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

我在 Windows 10 上使用 VS2008。 我对这个功能有疑问,希望你能帮助我

void CPythonNetworkStream::AppearShopSign(DWORD dwVID, std::string stSign) {
    if (stSign.empty())
        for (auto it = m_mapShopSign.begin(); it != m_mapShopSign.end(); ++it)
            if (dwVID == it->first)
                stSign = it->second;

    // LogBoxf("AppearShopSign: %u-%s", dwVID, stSign.c_str());
    PyCallClassMemberFunc(m_apoPhaseWnd[PHASE_WINDOW_GAME], "BINARY_PrivateShop_Appear", Py_BuildValue("(is)", dwVID, stSign.c_str()));
}

错误信息

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   
error C2440: 'initializing' : cannot convert from 'std::_Tree<_Traits>::iterator' to 'int' 
error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion)
error C2227: left of '->first' must point to class/struct/union/generic type

自从 c++11 标准以来支持 auto 关键字,而 visual-studio 2008 不提供该标准。

要解决这个问题,请使用显式类型:

for (std::map<DWORD,std::string>::iterator it = m_mapShopSign.begin(); 
  // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     it != m_mapShopSign.end(); 
     ++it)

如前所述,auto 是在 C++11 中为 automatic type deduction 引入的。在 C++11 之前,它被用来声明一个具有自动存储持续时间的局部变量。