错误:'words'前缺少模板参数
error: missing template arguments before 'words'
这是一个程序,它从用户那里读取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;
};
};
名称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;
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 错误:'words'前缺少模板参数