0
我創建了一個哈希表,設置array
的forward lists
的pairs
來保存數據。錯誤來自Gnu編譯器創建鏈接對列表的向量時:無效使用'::'
在C++術語聲明的std::pair
std::forward_list
一個std::vector
,如下所示:
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
(編輯:完整的示例下面張貼)
上面的線編譯在Visual Studio和Xcode的,但Gnu編譯器會引發以下錯誤。有任何想法嗎?謝謝。基思:^)
[~/pickledEgg] $ g++ -std=c++11 main.cpp -o hash
main.cpp:39:122: error: invalid use of ‘::’
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:122: error: expected ‘;’ at end of member declaration
main.cpp:39:128: error: expected unqualified-id before ‘>>’ token
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:112: error: wrong number of template arguments (1, should be 2)
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/include/c++/4.8.2/ios:40,
from /usr/include/c++/4.8.2/ostream:38,
from /usr/include/c++/4.8.2/iostream:39,
from main.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:96:12: error: provided for ‘template<class _T1, class _T2> struct std::pair’
struct pair
^
main.cpp:39:107: error: template argument 1 is invalid
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:107: error: template argument 2 is invalid
main.cpp:39:89: error: template argument 1 is invalid
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
^
main.cpp:39:89: error: template argument 2 is invalid
[~/QuickBase] $
完整的例子
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <forward_list>
#include <utility>
#include <chrono>
namespace qb {
const int HASHSIZE{ 7 };
class HashTable {
public:
bool insert(int key, std::string value) {
if (get(key) == "") { // Duplicate handling: ignore if exists
std::pair<int, std::string> myPair(key, value);
int hashedKey = hash_(key);
it_ = table_[hashedKey].before_begin();
table_[hashedKey].emplace_after(it_, myPair);
}
return true;
}
std::string get(int key) {
for (it_ = table_[hash_(key)].begin(); it_ != table_[hash_(key)].end(); ++it_)
if (it_->first == key)
return it_->second;
return "";
};
void print() {
for (int i = 0; i < HASHSIZE; ++i) {
std::cout << "BUCKET " << i << ": ";
for (it_ = table_[i].begin(); it_ != table_[i].end(); ++it_)
std::cout << "[" << it_->first << ", " << it_->second << "]" << " --> ";
std::cout << "NULL" << std::endl;
}
}
private:
std::vector<std::forward_list<std::pair<int, std::string>>> table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
std::forward_list<std::pair<int, std::string>>::iterator it_;
// Hashing function
int hash_(int key) {
return (key % HASHSIZE);
}
};
}
int main() {
qb::HashTable h;
std::cout << "Buckets: " << qb::HASHSIZE << std::endl << std::endl;
// Insertion
std::wcout << "INSERTION" << std::endl << std::endl;
h.insert(42, "beta");
h.insert(0, "gamma");
h.insert(32767, "delta");
h.insert(29, "epsilon");
h.insert(123, "zeta");
h.insert(74, "heta");
h.insert(1, "theta");
h.insert(42, "iota");
h.insert(33, "kappa");
h.insert(947, "lambda");
h.insert(10225, "mu");
h.print();
std::cout << std::endl;
// Retrieval
std::wcout << "RETRIEVAL" << std::endl << std::endl;
std::cout << "Get 29: " << h.get(29) << std::endl;
std::cout << "Get 34: " << h.get(34) << std::endl;
std::cout << "Get 10225: " << h.get(10225) << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Enter 'e' and press Enter to end: ";
std::cin.get();
std::cout << "\n\n";
return 0;
}
你有沒有'#包括','<修飾符Modifiers>',''和''? –
aschepler
無法複製,在我的海灣合作委員會爲我工作,並在https://godbolt.org/g/gyADCu – ephemient
我做到了;除了我用''#include''而不是''#include '',其中包含std :: pair庫這就是說,我將其更改爲,但得到相同的錯誤。 –
kmiklas