C++ 无法识别模板函数
由于某些原因,我的功能没有被正确识别。当我运行我的程序时,我得到一个错误,说该函数需要4个参数,并且提供了1个。下面的代码都是一个maini.cpp,我不知道为什么它会被分成不同的块。但您可以看到,在int main()中,我调用search(hTable)。这就是我收到错误的地方,即使它与模板函数搜索相匹配。 我得到的错误是:C++ 无法识别模板函数,c++,class,templates,hash,C++,Class,Templates,Hash,由于某些原因,我的功能没有被正确识别。当我运行我的程序时,我得到一个错误,说该函数需要4个参数,并且提供了1个。下面的代码都是一个maini.cpp,我不知道为什么它会被分成不同的块。但您可以看到,在int main()中,我调用search(hTable)。这就是我收到错误的地方,即使它与模板函数搜索相匹配。 我得到的错误是: c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algo.h:4024:5: note: template
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_algo.h:4024:5: note: template argument deduction/substitution failed:
main.cpp:40:18: note: candidate expects 4 arguments, 1 provided
search(hTable);
我认为我正确地声明了这两个函数,但显然我遗漏了一些东西。谢谢
#include "wordList.h"
#include "grid.h"
#include "heap.h"
#include "hashTable.h"
#include <time.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void findMatches(wordList list, grid theGrid);
void search(int typeSort, wordList list);
template <class T>
void findMatches(hashTable <T> list, grid theGrid);
template <class T>
void search(hashTable <T> list);
int main() {
wordList list1;
list1.read("wordlist.txt"); //create
int search1 = 1; //Insertion Sort
int search2 = 2; //Quick Sort
int search3 = 3; //Merge Sort
//Search function sorts the word list, then calls findMatches to find all words in the grid
search(search1, list1);
search(search2, list1);
search(search3, list1);
hashTable <string> hTable();
search(hTable);
return 0;
}
void search(int & typeSort, wordList & list) {
string gridName;
cout << "Enter name of grid file" << endl;
cin>>gridName;
grid theGrid;
theGrid.buildGrid(gridName); //build grid based on input
clock_t startSort, startSearch; //keep track of time
startSort = clock();
float insertionDiff, quickDiff, mergeDiff, searchDiff;
float totaltime;
switch (typeSort) {
case 1: cout << "Insertion Sort: " << endl;
list.insertionSort(); //call insertion sort on word list
insertionDiff = clock() - startSort; //print time from start to end of sorting
cout << "Time for Insertion Sort: " << (float) insertionDiff / CLOCKS_PER_SEC << endl;
break;
case 2: cout << "Quick Sort: " << endl;
list.quickSort(); //call quick sort on word list
quickDiff = clock() - startSort; //print time from start to end of sorting
cout << "Time for Quick Sort: " << (float) quickDiff / CLOCKS_PER_SEC << endl;
break;
case 3: cout << "Merge Sort: " << endl;
list.mergeSort(); //call merge sort on word list
mergeDiff = clock() - startSort; //print time from start to end of sorting
cout << "Time for Merge Sort: " << (float) mergeDiff / CLOCKS_PER_SEC << endl;
break;
}
startSearch = clock();
findMatches(list, theGrid); //call find matches to print all words found
searchDiff = clock() - startSearch;
cout << "Time for finding words: " << ((float) searchDiff / CLOCKS_PER_SEC) << endl; //print time for finding words
totaltime = clock() - startSort;
cout << "Total time for sorting and searching: " << totaltime / CLOCKS_PER_SEC << endl; //print time for sorting and searching
}
template<class T>
void search(hashTable <T> list) {
string gridName;
cout << "Enter name of grid file" << endl;
cin>>gridName;
grid theGrid;
theGrid.buildGrid(gridName); //build grid based on input
clock_t startHash, startSearch; //keep track of time
startHash = clock();
float hashDiff, searchDiff, totalTime;
float totaltime;
list.hashTable("wordlist.txt");
hashDiff = clock() - startHash;
cout << "Time to build hash table:" << hashDiff / CLOCKS_PER_SEC << endl;
startSearch = clock();
findMatches(list, theGrid);
searchDiff = clock() - startSearch;
cout << "Time to find words:" << searchDiff / CLOCKS_PER_SEC << endl;
totalTime = clock() - startHash;
cout << "Total time:" << totalTime / CLOCKS_PER_SEC << endl;
}
#包括“wordList.h”
#包括“grid.h”
#包括“heap.h”
#包括“hashTable.h”
#包括
#包括
#包括
#包括
使用名称空间std;
作废查找匹配项(单词列表、网格和网格);
无效搜索(int-typeSort,单词列表);
模板
void find匹配(哈希表列表、网格和网格);
模板
无效搜索(哈希表列表);
int main(){
词表列表1;
list1.read(“wordlist.txt”);//创建
int search1=1;//插入排序
int search2=2;//快速排序
int search3=3;//合并排序
//搜索函数对单词列表进行排序,然后调用findMatches查找网格中的所有单词
搜索(搜索1,列表1);
搜索(搜索2,列表1);
搜索(搜索3,列表1);
哈希表hTable();
搜索(hTable);
返回0;
}
无效搜索(int和typeSort、wordList和list){
字符串gridName;
coutgridname;
网格网格;
buildGrid(gridName);//基于输入构建网格
时钟\u t startSort,startSearch;//记录时间
startSort=clock();
float insertionDiff、quickDiff、mergeDiff、searchDiff;
浮动总时间;
开关(类型排序){
案例1:cout您可以将您的函数声明为模板1。
template <class T>
void search(hashTable <T> list);
在你的问题中粘贴错误和导致错误的代码。我们不需要整个程序,只需要出现问题的部分。@SamCraig你完全正确,我更新了问题
search(hTable);