简体   繁体   English

警告:基于范围的 for 循环是 C++11 扩展 [-Wc++11-extensions]

[英]warning: range-based for loop is a C++11 extension [-Wc++11-extensions]

hello my code is having the following issue: warning: range-based for loop is a C++11 extension [-Wc++11-extensions] for this line of code: for (auto val : myTable[i]) How do I fixed this?你好,我的代码有以下问题: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]这行代码warning: range-based for loop is a C++11 extension [-Wc++11-extensions]for (auto val : myTable[i]) How do我解决了这个? I couldn't find anything helpful online so I would appreciate step by step guidance (preferably with pictures but I won't complain).我在网上找不到任何有用的东西,所以我会很感激分步指导(最好有图片,但我不会抱怨)。

full code:完整代码:

#include <iostream>
#include<list>
using namespace std;


class hashtable
{

    int capacity;
    list<int> *myTable;

    public:

        hashtable(int capacity)
        {
            this->capacity = capacity;
            myTable = new list<int>[capacity];
        }

        void setList(int hashedIndex, int value)
        {
            myTable[hashedIndex].push_back(value);
        }

        int hashFunction(int value) {
            int retVal = (value * 31) % capacity;
            return retVal;
        }

        void insert(int value) {
            int hashedIndex = hashFunction(value);

            cout << "inserted " << value << endl;
            setList(hashedIndex, value);
        }

        void delete_elem(int value)
        {
            if (search(value))
            {
                int hashedIndex = hashFunction(value);
                myTable[hashedIndex].remove(value);
            }
        }

        bool search(int value)
        {
            int hashedIndex = hashFunction(value);
            list<int> ::iterator tmp;

            for (list<int> ::iterator itr = myTable[hashedIndex].begin(); itr != myTable[hashedIndex].end(); itr++)
            {
                if (*itr == value)
                {
                    tmp = itr;
                    break;
                }
            }

            return tmp != myTable[hashedIndex].end();
        }

        void printContents() {

            cout << "trying to print contents" << endl;
            for (int i = 0; i < capacity; i++)
            {
                cout << i;
                for (auto val : myTable[i]) // issue is on this line
                {
                    cout << " --> " << val;
                }
                cout << endl;
            }
            cout << endl;
        }
};

int main(int argc, char const *argv[])
{

    hashtable ht(10);

    for (int i = 0; i < 10; i++) {
        ht.insert(i);
    }

    ht.printContents();

    // cout << "yo" << endl;

    return 0;
}

The IDE I'm using is NetBeans我使用的 IDE 是 NetBeans

Activate c++11 in your compiler option and the warning will disappear :)在编译器选项中激活 c++11,警告将消失:)

It's just a "new" syntax, relative to the C++ 2011 norm, so unless you need backward compatibility with old compilers, you can safely enable c++11 support, or even c++14 or 17.相对于 C++ 2011 规范,这只是一种“新”语法,因此除非您需要向后兼容旧编译器,否则您可以安全地启用 c++11 支持,甚至 c++14 或 17。


To configure Netbeans, I quote Ferenc Géczi's answer :为了配置 Netbeans,我引用了Ferenc Géczi 的回答

  1. Make sure that you have pointed NetBeans to the correct MinGW version.确保您已将 NetBeans 指向正确的 MinGW 版本。 To do that, go to Project Properties > Build > Tool Collection > ... > Tool Collection Manager and there you can set the path to the proper g++ version.为此,请转到Project Properties > Build > Tool Collection > ... > Tool Collection Manager然后您可以将路径设置为正确的 g++ 版本。

  2. Make sure that you have set the correct compiler options:确保您设置了正确的编译器选项:

    Project Properties > Build > C++ Compiler > Project Properties > Build > C++ Compiler >

    Compilation Line > Additional Options Compilation Line > Additional Options

    set it to: -std=c++11将其设置为: -std=c++11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM