【问题标题】:Inserting an object of struct into a hash table C++将结构对象插入哈希表 C++
【发布时间】:2014-04-21 05:51:49
【问题描述】:

我目前正在从事一个涉及哈希表的项目。我已将文本文件读入符号结构的向量中,现在我必须将这些结构(实际上是对象)插入到哈希表中。我被赋予了一个特定的插入函数来使用,但是,我似乎无法让它工作。我已经包含了下面的插入函数以及我的 Driver.cpp 文件,该文件包含将文件读取到结构向量以及我尝试将其插入哈希表的尝试。

如果我出错的地方有任何帮助/反馈,我将不胜感激。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <time.h> 
#include <unistd.h>
#include "SeparateChaining.h" 

using namespace std;

struct Symbol
{
    int type;
    string name;
};

size_t hash(const string & key); //declaration of hashing function

int main()
{
    /************ VARS ***************/
    string line;
    int line_count;
    int table_size;
    int increment;

    /************ Vector of Symbols ***************/
    vector<Symbol> symbols;

    /************ HashTable of Symbols ***************/
    HashTable<Symbol> hashtable; //precomputed to have 101 elements --> probably will change this to accomodate table size.

    cout << "Opening file..." << endl;
    usleep(2000000);

    ifstream file;
    file.open("symbols.txt");

    if(!file)
    {
        cout << "System failed to open file.";
    }
    else
    {
        cout << "File successfully opened" << endl;
    }

    cout << "Please enter the table size for the Hash Table. (NOTE: It MUST be a prime number.)" << endl;
    cin >> table_size;

    for(Symbol temp; file >> temp.name >> temp.type;)
    {
        symbols.push_back(temp);
        increment++;
    }
    //Just to test and see if its loading it correctly...
    for(int i = 0; i < symbols.size(); i++)
    {
        cout << symbols[i].name << endl;
        cout << symbols[i].type << endl;
    }
    for(int j = 0; j < symbols.size(); j++)
    {
        hashtable.insert(symbols); // Messing up here !!!!!!!!!!!!!
    }
    //cout << ::hash("hi") << endl;
}

size_t hash( const string & key )
{
    size_t hashVal = 0;

    for( char ch : key )
        hashVal = 37 * hashVal + ch;

    return hashVal;
}

在SeparateChaining.h 中插入函数:

bool insert( const HashedObj & x )
    {
        auto & whichList = theLists[ myhash( x ) ];
        if( find( begin( whichList ), end( whichList ), x ) != end( whichList) )
            return false;
        whichList.push_back( x );

            // Rehash; see Section 5.5
        if( ++currentSize > theLists.size( ) )
            rehash( );

        return true;
    }

    bool insert( HashedObj && x )
    {
        auto & whichList = theLists[ myhash( x ) ];      
        if( find( begin( whichList ), end( whichList ), x ) != end( whichList ) )
            return false;
        whichList.push_back( std::move( x ) );

            // Rehash; see Section 5.5
        if( ++currentSize > theLists.size( ) )
            rehash( );

        return true;
    }

作为 hashtable.insert(symbols) 完成时的错误:

    Driver.cpp: In function 'int main()':
    Driver.cpp:65:27: error: no matching function for call to 'HashTable<Symbol>::insert(std::vector<Symbol>&)'
    Driver.cpp:65:27: note: candidates are:
    In file included from Driver.cpp:8:0:
    SeparateChaining.h:44:10: note: bool HashTable<HashedObj>::insert(const HashedObj&) [with HashedObj = Symbol]
    SeparateChaining.h:44:10: note:   no known conversion for argument 1 from 'std::vector<Symbol>' to 'const Symbol&'
    SeparateChaining.h:58:10: note: bool HashTable<HashedObj>::insert(HashedObj&&) [with HashedObj = Symbol]
    SeparateChaining.h:58:10: note:   no known conversion for argument 1 from 'std::vector<Symbol>' to 'Symbol&&'

通过向量递增后作为 hashtable.insert(symbols[j]) 完成时的错误:

In file included from /opt/local/include/gcc47/c++/bits/basic_string.h:3032:0,
                             from /opt/local/include/gcc47/c++/string:54,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/functional_hash.h: In instantiation of 'struct std::hash<Symbol>':
            SeparateChaining.h:107:32:   required from 'size_t HashTable<HashedObj>::myhash(const HashedObj&) const [with HashedObj = Symbol; size_t = long unsigned int]'
            SeparateChaining.h:46:50:   required from 'bool HashTable<HashedObj>::insert(const HashedObj&) [with HashedObj = Symbol]'
            Driver.cpp:65:30:   required from here
            /opt/local/include/gcc47/c++/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h: In instantiation of '_InputIterator std::__find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_List_iterator<Symbol>; _Tp = Symbol]':
            /opt/local/include/gcc47/c++/bits/stl_algo.h:4466:45:   required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = std::_List_iterator<Symbol>; _Tp = Symbol]'
            SeparateChaining.h:47:9:   required from 'bool HashTable<HashedObj>::insert(const HashedObj&) [with HashedObj = Symbol]'
            Driver.cpp:65:30:   required from here
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: error: no match for 'operator==' in '__first.std::_List_iterator<_Tp>::operator*<Symbol>() == __val'
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note: candidates are:
            In file included from /opt/local/include/gcc47/c++/bits/stl_algo.h:68:0,
                             from /opt/local/include/gcc47/c++/algorithm:63,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/functional:2382:5: note: template<class _Res, class ... _Args> bool std::operator==(std::nullptr_t, const std::function<_Res(_ArgTypes ...)>&)
            /opt/local/include/gcc47/c++/functional:2382:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   cannot convert '__first.std::_List_iterator<_Tp>::operator*<Symbol>()' (type 'Symbol') to type 'std::nullptr_t'
            In file included from /opt/local/include/gcc47/c++/bits/stl_algo.h:68:0,
                             from /opt/local/include/gcc47/c++/algorithm:63,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/functional:2376:5: note: template<class _Res, class ... _Args> bool std::operator==(const std::function<_Res(_ArgTypes ...)>&, std::nullptr_t)
            /opt/local/include/gcc47/c++/functional:2376:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::function<_Res(_ArgTypes ...)>'
            In file included from /opt/local/include/gcc47/c++/functional:56:0,
                             from /opt/local/include/gcc47/c++/bits/stl_algo.h:68,
                             from /opt/local/include/gcc47/c++/algorithm:63,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/tuple:797:5: note: template<class ... _TElements, class ... _UElements> bool std::operator==(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)
            /opt/local/include/gcc47/c++/tuple:797:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::tuple<_Elements ...>'
            In file included from /opt/local/include/gcc47/c++/random:51:0,
                             from /opt/local/include/gcc47/c++/bits/stl_algo.h:67,
                             from /opt/local/include/gcc47/c++/algorithm:63,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/random.tcc:1713:5: note: template<class _RealType1> bool std::operator==(const std::normal_distribution<_RealType>&, const std::normal_distribution<_RealType>&)
            /opt/local/include/gcc47/c++/bits/random.tcc:1713:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::normal_distribution<_RealType>'
            In file included from /opt/local/include/gcc47/c++/list:64:0,
                             from SeparateChaining.h:6,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_list.h:1574:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
            /opt/local/include/gcc47/c++/bits/stl_list.h:1574:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::list<_Tp, _Alloc>'
            In file included from /opt/local/include/gcc47/c++/list:64:0,
                             from SeparateChaining.h:6,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_list.h:277:5: note: template<class _Val> bool std::operator==(const std::_List_iterator<_Tp>&, const std::_List_const_iterator<_Val>&)
            /opt/local/include/gcc47/c++/bits/stl_list.h:277:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::_List_iterator<_Tp>'
            In file included from /opt/local/include/gcc47/c++/vector:65:0,
                             from Driver.cpp:4:
            /opt/local/include/gcc47/c++/bits/stl_vector.h:1370:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
            /opt/local/include/gcc47/c++/bits/stl_vector.h:1370:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::vector<_Tp, _Alloc>'
            In file included from /opt/local/include/gcc47/c++/bits/locale_facets.h:50:0,
                             from /opt/local/include/gcc47/c++/bits/basic_ios.h:39,
                             from /opt/local/include/gcc47/c++/ios:45,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/streambuf_iterator.h:206:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
            /opt/local/include/gcc47/c++/bits/streambuf_iterator.h:206:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::istreambuf_iterator<_CharT, _Traits>'
            In file included from /opt/local/include/gcc47/c++/string:54:0,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/basic_string.h:2516:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
            /opt/local/include/gcc47/c++/bits/basic_string.h:2516:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
            In file included from /opt/local/include/gcc47/c++/string:54:0,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/basic_string.h:2504:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
            /opt/local/include/gcc47/c++/bits/basic_string.h:2504:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   mismatched types 'const _CharT*' and 'Symbol'
            In file included from /opt/local/include/gcc47/c++/string:54:0,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/basic_string.h:2490:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
            /opt/local/include/gcc47/c++/bits/basic_string.h:2490:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::basic_string<_CharT>'
            In file included from /opt/local/include/gcc47/c++/string:54:0,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/basic_string.h:2483:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
            /opt/local/include/gcc47/c++/bits/basic_string.h:2483:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
            In file included from /opt/local/include/gcc47/c++/string:43:0,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/allocator.h:124:5: note: template<class _Tp> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
            /opt/local/include/gcc47/c++/bits/allocator.h:124:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::allocator<_CharT>'
            In file included from /opt/local/include/gcc47/c++/string:43:0,
                             from /opt/local/include/gcc47/c++/bits/locale_classes.h:42,
                             from /opt/local/include/gcc47/c++/bits/ios_base.h:43,
                             from /opt/local/include/gcc47/c++/ios:43,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/allocator.h:119:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)
            /opt/local/include/gcc47/c++/bits/allocator.h:119:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::allocator<_CharT>'
            In file included from /opt/local/include/gcc47/c++/bits/stl_algobase.h:68:0,
                             from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
                             from /opt/local/include/gcc47/c++/ios:41,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:1039:5: note: template<class _Iterator> bool std::operator==(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:1039:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::move_iterator<_Iterator>'
            In file included from /opt/local/include/gcc47/c++/bits/stl_algobase.h:68:0,
                             from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
                             from /opt/local/include/gcc47/c++/ios:41,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:1033:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:1033:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::move_iterator<_Iterator>'
            In file included from /opt/local/include/gcc47/c++/bits/stl_algobase.h:68:0,
                             from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
                             from /opt/local/include/gcc47/c++/ios:41,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:343:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:343:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::reverse_iterator<_Iterator>'
            In file included from /opt/local/include/gcc47/c++/bits/stl_algobase.h:68:0,
                             from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
                             from /opt/local/include/gcc47/c++/ios:41,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:293:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
            /opt/local/include/gcc47/c++/bits/stl_iterator.h:293:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::reverse_iterator<_Iterator>'
            In file included from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65:0,
                             from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
                             from /opt/local/include/gcc47/c++/ios:41,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/stl_pair.h:206:5: note: template<class _T1, class _T2> constexpr bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
            /opt/local/include/gcc47/c++/bits/stl_pair.h:206:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::pair<_T1, _T2>'
            In file included from /opt/local/include/gcc47/c++/iosfwd:42:0,
                             from /opt/local/include/gcc47/c++/ios:39,
                             from /opt/local/include/gcc47/c++/ostream:40,
                             from /opt/local/include/gcc47/c++/iostream:40,
                             from Driver.cpp:1:
            /opt/local/include/gcc47/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
            /opt/local/include/gcc47/c++/bits/postypes.h:218:5: note:   template argument deduction/substitution failed:
            In file included from /opt/local/include/gcc47/c++/algorithm:63:0,
                             from SeparateChaining.h:8,
                             from Driver.cpp:8:
            /opt/local/include/gcc47/c++/bits/stl_algo.h:135:7: note:   'Symbol' is not derived from 'const std::fpos<_StateT>'

【问题讨论】:

  • 您遇到了什么具体问题?
  • 显然插入函数需要一个对象。我的向量是对象向量,但是当我尝试通过 hashtable.insert(symbols) 插入这些对象时,它会向我吐出一堆错误。现在我不知道是我只是插入了错误的东西还是以错误的方式做? @Wyzard
  • 什么是“一堆错误”?您需要在问题中包含这些详细信息。不要仅仅为了找出问题的症状而要求人们编译和运行你的程序。
  • @Wyzard 我很乐意发布错误,我只是想知道我是否正确地执行了此操作。我在上面重新发布了与他们的情况相匹配的以下错误。

标签: c++ struct hashtable


【解决方案1】:

试试

hashtable.insert(symbols[j]);

另外,您不使用incrementline_counttable_size,您只需设置它们即可。

说明: 您试图插入整个向量,而不是其元素,因此数据类型不匹配。通过使用数组表示法,您可以获取要插入的实际元素。

为什么你甚至使用向量而不是将符号直接插入哈希表?

【讨论】:

  • 我尝试做符号[j] 但是,我得到了如上所示的错误。
  • 哦,我明白了。您是否将您的哈希类型专门用于 Symbol 类型?请参阅link,页面上的最后一个示例
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2015-06-25
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
相关资源
最近更新 更多