简体   繁体   中英

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? 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

Activate c++11 in your compiler option and the warning will disappear :)

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.


To configure Netbeans, I quote Ferenc Géczi's answer :

  1. Make sure that you have pointed NetBeans to the correct MinGW version. 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.

  2. Make sure that you have set the correct compiler options:

    Project Properties > Build > C++ Compiler >

    Compilation Line > Additional Options

    set it to: -std=c++11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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