Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ HashMap-插入类型错误_C++_Templates - Fatal编程技术网

C++ HashMap-插入类型错误

C++ HashMap-插入类型错误,c++,templates,C++,Templates,在我的main方法中,以下代码在包含插入的行中有一个错误 hashTable<string, pair<string, string>> friendsHash = hashTable<string, pair<string, string>>(friendTotal); if(critChoice == 1) { for(int counter = 0; counter < frien

在我的main方法中,以下代码在包含插入的行中有一个错误

hashTable<string, pair<string, string>> friendsHash = hashTable<string, pair<string, string>>(friendTotal);
        if(critChoice == 1)
        {
            for(int counter = 0; counter < friendTotal; counter ++)
            {
                string name = friends[counter].getName();
                string date = friends[counter].getBirthDate();
                string homeTown = friends[counter].getHomeTown();
                friendsHash.insert(pair<name, pair<date, homeTown>>);
            }
        }
hashTable-friendsHash=hashTable(friendTotal);
if(critChoice==1)
{
对于(int counter=0;counter
hashMap的插入函数如下所示:

template<class K, class E>
void hashTable<K, E>::insert(const pair<const K, E>& thePair)
{
    int b = search(thePair.first);

    //check if matching element found
    if(table[b] == NULL)
    {
        //no matching element and table not full
        table[b] = new pair<const K, E> (thePair);
        dSize ++;
    }
    else
    {//check if duplicate or table full
        if(table[b]->first == thePair.first)
        {//duplicate, change table[b]->second
            table[b]->second = thePair.second;
        }
        else //table is full
            throw hashTableFull();
    }
}
模板
void哈希表::insert(常量对和指针)
{
int b=搜索(首先搜索空气);
//检查是否找到匹配的元素
如果(表[b]==NULL)
{
//没有匹配的元素,表未满
表[b]=新的一对(thePair);
dSize++;
}
其他的
{//检查是否重复或表已满
if(表[b]->first==thePair.first)
{//重复,更改表[b]->秒
表[b]->second=thePair.second;
}
否则,桌子就满了
抛出hashTableFull();
}
}

错误在于插入函数调用
中的3个参数都不是参数的有效模板类型参数

您混淆了实例化类模板以获取类型的语法,以及实例化类型以获取对象的语法

pair<name, pair<date, homeTown>>
或者如果你能使用C++11

{name, {date, homeTown}}

您正在混淆用于实例化类模板以获取类型的语法,以及用于实例化类型以获取对象的语法

pair<name, pair<date, homeTown>>
或者如果你能使用C++11

{name, {date, homeTown}}
在这方面:

friendsHash.insert(pair<name, pair<date, homeTown>>);
助手函数模板
make_pair()
能够推断其参数的类型,并生成
pair
的正确实例化,从而减轻了显式指定模板参数的负担。

在这一行:

friendsHash.insert(pair<name, pair<date, homeTown>>);

助手函数模板
make_pair()
能够推断其参数的类型,并生成
pair
的正确实例化,从而减轻了显式指定模板参数的负担。

第一行可以缩短为
哈希表friendsHash(friendTotal)第一行可以缩短为
哈希表friendsHash(friendTotal)