【问题标题】:ISO C++ forbids declaration of ‘it’ with no type for auto iterator?ISO C++ 禁止声明没有自动迭代器类型的“it”?
【发布时间】:2012-12-08 04:34:37
【问题描述】:

我有这六行:

    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 上解决这些错误?

【问题讨论】:

  • auto 只推导出 C++11 中的类型。它是 C++03 中的默认存储类型,而不是 register 等。

标签: c++ compiler-errors iterator auto


【解决方案1】:

编译时启用 C++11 模式。为此,您可以在编译器标志中添加-std=gnu++11(也可以获得默认启用的 GCC 扩展)或-std=c++11(仅适用于 ISO C++)。

auto 在 C++11 中的含义(它推导类型)与以前的标准(它指定自动存储类)中的含义不同。

【讨论】:

  • 我相信标志是-std=gnu++11
猜你喜欢
  • 2010-12-16
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多