由于某种原因,我的功能无法正确识别。当我运行我的程序时,我得到一个错误,说该函数需要4个参数,并提供1.下面的代码都是一个maini.cpp,我不知道为什么它被分解成不同的块。但你可以看到在int main()中我调用了搜索(hTable)。这是我收到错误的地方,即使它与模板功能搜索匹配。 我得到的错误是:
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;
}
答案 0 :(得分:0)
您将您的功能声明为模板一。
template <class T>
void search(hashTable <T> list);
但它没有任何模板参数使用....
search(hTable);