哈希表解释器
Hash Table Interpreter
我正在尝试制作一个哈希表解释器,遇到一些错误。
我收到这个错误,每次我使用hashString。
main.cpp:99:43: error: use of undeclared identifier 'hashString'
temp.next = table[hashString(temp.getName())];
^
main.cpp:100:31: error: use of undeclared identifier 'hashString'
table[hashString(temp.getName())] = temp;
我尝试声明hashstring(注释掉),但它只是给了我更多的错误,像这样:
main.cpp:105:34: error: no viable overloaded operator[] for type 'node [7]'
欢迎提出任何建议。谢谢!
我代码:class node{
public:
node();
node* next;
string getName();
int getNum();
int getScope();
void setName(string);
void setNum(int);
void setScope(int);
int hashString(string s);
private:
string name;
int num;
int scope;
};
node::node(){
next = 0;
name = "";
num = 0;
scope = 0;
}
string node::getName(){
return name;
}
int node::getNum(){
return num;
}
int node::getScope(){
return scope;
}
void node::setName(string s){
name = s;
}
void node::setScope(int x){
scope = x;
}
void node::setNum(int x){
num = x;
}
int node::hashString(string s){
int hash=0,size = s.size();
for(int i= 0;i<size+1;i++){
hash+= (int) s[i] * i;
}
return hash%7;
}
int main(){
int curr_scope=0,num_line=0;
node table[7];
//node hashString(string s);
ifstream myfile ("input.txt");
if (myfile.is_open()){
string s;
while (getline (myfile,s)){
num_line++;
stringstream line(s);
line >> s;
if(s == "START"){
curr_scope++;
while(line >> s);
}
else if(s == "FINISH"){
curr_scope--;
while(line >> s);
}
else if(s == "COM"){
while(line >> s);
}
else if(s == "VAR"){
node temp;
line >> s;
cout << s << endl;
temp.setName(s);
line >> s;
if(s == "="){
line >> s;
cout << s << endl;
temp.setNum(atoi(s.c_str()));
temp.setScope(curr_scope);
if(table[hashString(temp.getName())] != 0){
temp.next = table[hashString(temp.getName())];
table[hashString(temp.getName())] = temp;
while(line >> s);
}
else if(table[hashString(temp.getName())] == 0){
table[hashString(temp.getName())] = temp;
while(line >> s);
}
else{
cout << "UNABLE TO ADD " << temp.getName() << "TO THE TABLE" << endl;
while(line >> s);
}
}
}
else if(s == "PRINT")
{
line >> s;
node temp = table[hashString(s)];
if(temp.getScope() == curr_scope){
if(line >> s){
if(s == "++"){
cout << temp.getName() << " IS " << temp.getNum() + 1 << endl;
while(line >> s);
}
else if(s == "--"){
cout << temp.getName() << " IS " << temp.getNum() - 1 << endl;
while(line >> s);
}
else if(s == "+"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() + atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "-"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() - atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "/"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() / atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "*"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() * atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "%"){
line >> s;
cout << temp.getName() << " IS " << temp.getNum() % atoi(s.c_str()) << endl;
while(line >> s);
}
else if(s == "^"){
line >> s;
cout << temp.getName() << " IS " << pow(temp.getNum(),atoi(s.c_str())) << endl;
while(line >> s);
}
}
}
else{
cout << s << "IS UNDEFINED" << endl;
cout << "ERROR HAS OCCURED ON LINE " << num_line << endl;
while(line >> s);
}
}
else{
if(table[hashString(s)].getName == s){
node temp = table[hashString(s)];
line >> s;
if(s == "="){
if(temp.getScope() == curr_scope){
line >> s;
table[hashString(temp)].setNum(atoi(s.c_str()));
while(line >> s);
}
}
else if(s == "++"){
table[hashString(temp)].setNum(table[hashString(temp)].getNum()+1);
while(line >> s);
}
else if(s == "--"){
table[hashString(temp)].setNum(table[hashString(temp)].getNum()-1);
while(line >> s);
}
}
else
cout << s << "IS UNDEFINED" << endl;
}
}
myfile.close();
}
system("PAUSE");
return 0;
}
为什么hashString
是Node的一部分?
如果没有原因,那么您应该将其移动到一个自由函数,这将解决您的问题。
否则,您需要在node
实例上调用它,如temp.hashString(s)
。
您也可以使它成为一个类静态函数,以便您可以这样调用它:node::hashString(s)
,如果您想使它与节点类相关联。
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- C++中的并发哈希表
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 调整大小和复制哈希表数组中的元素
- 带链接的基本哈希表
- C++哈希表中,两个相同的实现,但一个给出错误
- 如果索引不是整数,我们如何在 C++ 中插入哈希表
- 查找项目在哈希表中的位置
- 为什么C++ STL 哈希表 (unordered_map) 不接受向量作为键
- C++哈希表 - 如何解决自定义数据类型作为键的unordered_map冲突?
- 使用哈希表设置实现
- 同时写入和读取哈希表
- 在 C++ 中为特定哈希表创建插入函数
- 我可以比朴素哈希表更快地将随机字符串映射到两个类吗?
- 哈希表:船舶记录
- 使用rehash的C++哈希表问题
- 哈希表解释器