ISO C ++禁止声明'it'没有自动迭代器的类型?

时间:2012-12-08 04:34:38

标签: c++ compiler-errors iterator auto

我有这六行:

    auto it = rcp_amxinfo.find(LocalPass.script);//175
    if (it != rcp_amxinfo.end()) //176
    {//177
        if(it->second.GPSRouteCalculated.PublicFound)//178
        {
            ...
            amx_Exec(LocalPass.script, NULL, it->second.GPSRouteCalculated.POINTER);//186

他们在VS2012中编译得很好,但是在centOS6上的GCC中我得到了这些错误:

./RouteConnector/main.cpp:175: error: ISO C++ forbids declaration of ‘it’ with no type
./RouteConnector/main.cpp:175: error: cannot convert ‘std::_Rb_tree_iterator<std::pair<AMX* const, Callbacks> >’ to ‘int’ in initialization
./RouteConnector/main.cpp:176: error: no match for ‘operator!=’ in ‘it != rcp_amxinfo.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = AMX*, _Tp = Callbacks, _Compare = std::less<AMX*>, _Alloc = std::allocator<std::pair<AMX* const, Callbacks> >]()’
./RouteConnector/main.cpp:178: error: base operand of ‘->’ is not a pointer
./RouteConnector/main.cpp:186: error: base operand of ‘->’ is not a pointer

rcp_amxinfo定义如下:

struct CallbackAMX
{
    bool PublicFound;
    int POINTER;
    CallbackAMX()
    {
        PublicFound = false;
        POINTER = 0;
    }
};

struct Callbacks
{
    CallbackAMX ClosestNodeIDChange;
    CallbackAMX GPSRouteCalculated;
};

std::map            <AMX*, Callbacks>               rcp_amxinfo;

如何在linux上解决这些错误?

1 个答案:

答案 0 :(得分:9)

构建时启用C ++ 11模式。您可以通过添加-std=gnu++11(以获取GCC扩展,默认情况下处于打开状态)或-std=c++11(仅适用于ISO C ++)添加到编译器标志中来实现。

auto表示C ++ 11中的不同之处(它推导出类型),而不是之前的标准(它指定自动存储类)。