创建对链表向量时 Gnu 编译器的错误:无效使用"::"

Error from Gnu compiler when creating vector of linked lists of pairs: invalid use of '::'

本文关键字:无效 错误 编译器 链表 向量 Gnu 创建      更新时间:2023-10-16

我正在创建一个哈希表,设置一个forward lists pairsarray来保存数据。

在C++条款中宣布std::vector std::forward_list std::pair,如下:

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 << "nn";
    return 0;
}

啊,我明白了。

发布的行中,我无意中在类定义中分配了一个值。创建对象时,需要在构造函数中完成,如下所示:

HashTable() {
    table_ = std::vector<std::forward_list<std::pair<int, std::string>>>(HASHSIZE);
}