這是一個程序,它從用戶讀取15個單詞,並在將它們存儲在數組中之前通過散列算法運行它們。然後將哈希值顯示給用戶,並且用戶可以查詢數組中的特定單詞。當我嘗試運行它,我得到如下編譯錯誤:錯誤:在'單詞'前缺少模板參數
11 error: missing template arguments before 'words'
11 error: expected ';' before 'words'
21 error: 'words' was not declared in this scope
27 error: 'words' was not declared in this scope
的代碼如下:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
int x;
string input;
hash words; //from class hash defined below
bool query;
bool found;
cout<<"Please enter 15 words to be stored in a database."<<endl;
for(x = 1; x <= 15; x++)
{
cout<<"Word "<<x<<": ";
cin>>input; //new word
words.addHash(input); //calls addHash function in hash class
}
cout<<"Here are the hashes: "<<endl;
words.display(); //calls display function in hash class
cout<<"You can now query the database for a specific word. To stop, type 'stop'."<<endl;
//search words database
do
{
cout<<"Query: ";
cin>>input;
//more specifically, this calls search function to query words for <user_input>
if (input != "stop")
{
found = words.search(input);
if (found == true)
{
cout<<"Yes, that word was found in the database."<<endl;
}
else
{
cout<<"No, that word was NOT found in the database." <<endl;
};
}
else
{
query = false;
};
} while ((query) && (input != "stop"));
return 0;
};
class hash {
private:
int y;
std::string hashes[23];
std::string newHash;
char first_letter;
char last_letter;
int location;
int z;
std::string queryHash;
bool found;
public:
//first, the constructor for the hash algorithm
hash()
{
for (y = 0; y < 23; y++)
{
hashes[y] = "_";
}
};
void addHash(std::string newHash)
{
first_letter = newHash.at(0);
last_letter = newHash.at(newHash.size() - 1);
location = ((((int)first_letter) + ((int)last_letter)) % 23);
do
{
if (location == 23)
{
location = 0;
}
else
{
location = location + 1;
}
hashes[location] = newHash;
} while (hashes[location] != "_");
};
//prints out ALL contents of the 'database'
void display()
{
for (z = 0; z < 23; z++)
{
cout<<hashes[z]<<endl;
}
};
//searches the 'database' for the specific word
bool search(std::string queryHash)
{
first_letter = queryHash.at(0);
last_letter = queryHash.at(queryHash.size() - 1);
location = ((((int)first_letter) + ((int)last_letter)) % 23);
do
{
if (location == 23)
{
location = 0;
}
else
{
location = location + 1;
}
hashes[location] = newHash;
} while ((hashes[location] != "_") && (hashes[location] != queryHash));
if (hashes[location] == queryHash)
{
found = true;
}
else
{
found = false;
}
return found;
};
};
更新:我modifyed如下面的代碼,但仍得到相同的錯誤。
#include <iostream>
#include <string>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
using std::string;
class hash; //forward declaration
int main()
{
int x;
string input;
hash words; //from class hash defined below
bool query;
bool found;
cout<<"Please enter 15 words to be stored in a database."<<endl;
for(x = 1; x <= 15; x++)
{
cout<<"Word "<<x<<": ";
cin>>input; //new word
words.addHash(input); //calls addHash function in hash class
}
cout<<"Here are the hashes: "<<endl;
words.display(); //calls display function in hash class
cout<<"You can now query the database for a specific word. To stop, type 'stop'."<<endl;
//search words database
do
{
cout<<"Query: ";
cin>>input;
//more specifically, this calls search function to query words for <user_input>
if (input != "stop")
{
found = words.search(input);
if (found == true)
{
cout<<"Yes, that word was found in the database."<<endl;
}
else
{
cout<<"No, that word was NOT found in the database." <<endl;
};
}
else
{
query = false;
};
} while ((query) && (input != "stop"));
return 0;
};
class hash {
private:
int y;
std::string hashes[23];
std::string newHash;
char first_letter;
char last_letter;
int location;
int z;
std::string queryHash;
bool found;
public:
//first, the constructor for the hash algorithm
hash()
{
for (y = 0; y < 23; y++)
{
hashes[y] = "_";
}
};
void addHash(std::string newHash)
{
first_letter = newHash.at(0);
last_letter = newHash.at(newHash.size() - 1);
location = ((((int)first_letter) + ((int)last_letter)) % 23);
do
{
if (location == 23)
{
location = 0;
}
else
{
location = location + 1;
}
hashes[location] = newHash;
} while (hashes[location] != "_");
};
//prints out ALL contents of the 'database'
void display()
{
for (z = 0; z < 23; z++)
{
cout<<hashes[z]<<endl;
}
};
//searches the 'database' for the specific word
bool search(std::string queryHash)
{
first_letter = queryHash.at(0);
last_letter = queryHash.at(queryHash.size() - 1);
location = ((((int)first_letter) + ((int)last_letter)) % 23);
do
{
if (location == 23)
{
location = 0;
}
else
{
location = location + 1;
}
hashes[location] = newHash;
} while ((hashes[location] != "_") && (hashes[location] != queryHash));
if (hashes[location] == queryHash)
{
found = true;
}
else
{
found = false;
}
return found;
};
};
當您在main中聲明散列時,未定義散列。你需要轉發宣佈這個類。 –