c++ - 对于(自动& x : unordered_map variable) - this statement throwing error

标签 c++ c++11

您好,我的代码片段如下

#include <iostream>
#include <string>
#include <unordered_map>

struct job
{
    int priority;
    int state;
    std::string name;
};
job* selectJob(std::unordered_map<int, job*> jobList)
{
    for (auto& x : jobList)
    {
        if(x->state == 1)
        return x;
    }
    return NULL;
}

int main()
{
    std::unordered_map<int, job*> jobList;
    job a = { 1, 1, "a" };
    jobList.insert(std::make_pair<int, job*>(1, &a));
    job *selected = NULL;

    while (NULL != (selected = selectJob(jobList)))
    {
        std::cout << "Name: " << selected->name << "Prio: " << selected->priority << std::endl;
        selected->state = 2;
    }
    return 0;
}

在 linux 上编译时会抛出错误:

g++ -std=gnu++0x q.cpp
q.cpp: In function âjob* selectJob(std::unordered_map<int, job*, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, job*> > >&)â:
q.cpp:13: error: a function-definition is not allowed here before â:â token
q.cpp:18: error: expected primary-expression before âreturnâ
q.cpp:18: error: expected `;' before âreturnâ
q.cpp:18: error: expected primary-expression before âreturnâ
q.cpp:18: error: expected `)' before âreturnâ

有人遇到过这个问题吗?

最佳答案

您使用的编译器版本 (gcc 4.3) 不支持自动变量。

http://gcc.gnu.org/gcc-4.3/cxx0x_status.html

自动类型变量 N1984 否

关于c++ - 对于(自动& x : unordered_map variable) - this statement throwing error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20711965/

相关文章:

c++ - CryptDecrypt 在解密字符串的末尾返回随机字符?

c++ - 如何更新来自 getter 的变量

c++ - 绘制具有透明度的位图

C++0x 编译器支持问题

c++ - 在 std::cout 刷新事件上捕获和引发事件

c++ - ACE 宏定义错误

c++ - Qt:设置后如何清除setFixedSize

c++ - boost 序列化 std::unique_ptr 支持

c++ - 可变参数模板类型推导

c++ - 返回并自动推导 std::initializer_list