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

问题描述 投票:-1回答:1

您好,我的代码遇到以下问题:此行代码的warning: range-based for loop is a C++11 extension [-Wc++11-extensions]for (auto val : myTable[i])我该如何解决?我在网上找不到任何有用的信息,因此,我希望逐步提供指导(最好提供图片,但我不会抱怨)。

完整代码:

#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;
}

我正在使用的IDE是NetBeans

c++ c++11 netbeans
1个回答
0
投票

在编译器选项中激活c ++ 11,警告将消失:)

相对于C ++ 2011规范,这只是“新”语法,因此,除非需要与旧编译器向后兼容,否则可以安全地启用c ++ 11支持,甚至可以启用c ++ 14或17。


要配置Netbeans,请引用Ferenc Géczi's answer

  1. 请确保您已将NetBeans指向正确的MinGW版本。为此,请转到Project Properties >Build>Tool Collection>...> Tool Collection Manager,然后在那里,您可以将路径设置为正确的g ++版本。

  2. 请确保您设置了正确的编译器选项:

    Project Properties >Build>C++ Compiler>

    Compilation Line > Additional Options

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

© www.soinside.com 2019 - 2024. All rights reserved.