2017-04-12 10 views
0

Ich erstelle eine Hash-Tabelle, eine array von forward lists von pairs einrichten, um die Daten zu halten.Fehler von Gnu-Compiler beim Erstellen von Vektor von verknüpften Listen von Paaren: ungültige Verwendung von '::'

In C++ Bedingungen ein std::vector von std::forward_list von std::pair erklärt, wie folgt:

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

(EDIT: Komplettes Beispiel unten geschrieben)

Die Linie kompiliert oben in Visual Studio und Xcode, aber die Der Gnu-Compiler gibt den folgenden Fehler aus. Irgendwelche Ideen? Danke. Keith: ^)

[~/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] $ 

COMPLETE Beispiel

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

Hast du '# include ', '', '' und '' ? – aschepler

+0

Kann nicht reproduzieren, funktioniert für mich in GCC lokal und auf https://godbolt.org/g/gyADCu – ephemient

+0

Ich tat; außer dass ich '' #include '' anstelle von '' #include '', die die std :: pair-Bibliothek enthält, dass gesagt, änderte ich es zu , aber den gleichen Fehler zu bekommen. – kmiklas

Antwort

0

Aargh, ich habe es.

In der Zeile gebucht, ich versehentlich einen Wert in der Klassendefinition zugewiesen. Es muss im Konstruktor durchgeführt werden, wenn das Objekt erstellt wird, etwa so: speziell:

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