这是一个程序,它从用户读取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;
};
};
更新:我修改了以下代码,但仍然遇到同样的错误。
#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;
};
};
答案 0 :(得分:0)
名称hash
与std::hash
冲突。
要纠正此问题,
using namespace std;
std::
中的每个标识符添加std
,或为每个标识符添加using std::cin;
之类的声明,而不是using namespace std;
更新:您的代码应如下所示:
#include <iostream>
#include <string>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
using std::string;
// delcaration of a class
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:
hash();
void addHash(std::string newHash);
void display();
bool search(std::string queryHash);
};
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;
}
// below are definitions of member functions
//first, the constructor for the hash algorithm
hash::hash()
{
for (y = 0; y < 23; y++)
{
hashes[y] = "_";
}
}
void hash::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 hash::display()
{
for (z = 0; z < 23; z++)
{
cout<<hashes[z]<<endl;
}
}
//searches the 'database' for the specific word
bool hash::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;
}