[英]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]
這行代碼warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
: for (auto val : myTable[i])
How do我解決了這個? 我在網上找不到任何有用的東西,所以我會很感激分步指導(最好有圖片,但我不會抱怨)。
完整代碼:
#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++11,警告將消失:)
相對於 C++ 2011 規范,這只是一種“新”語法,因此除非您需要向后兼容舊編譯器,否則您可以安全地啟用 c++11 支持,甚至 c++14 或 17。
為了配置 Netbeans,我引用了Ferenc Géczi 的回答:
確保您已將 NetBeans 指向正確的 MinGW 版本。 為此,請轉到Project Properties
> Build
> Tool Collection
> ...
> Tool Collection Manager
然后您可以將路徑設置為正確的 g++ 版本。
確保您設置了正確的編譯器選項:
Project Properties
> Build
> C++ Compiler
>
Compilation Line
> Additional Options
將其設置為: -std=c++11
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.