2015-10-26 81 views
0

我想做一个哈希表解释器,运行到一些错误。哈希表解释器

我收到此错误每次我使用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; 
} 
+0

hashString是一个成员函数,你不能像这样调用它。 –

+0

请发布[MCVE](https://stackoverflow.com/help/mcve) –

回答

0

为什么hashString节点的一部分?

如果没有一个理由,那么你应该将它移动到一个免费的功能,这将解决您的问题。

否则,你需要调用它在这样一个temp.hashString(s)实例node

你也可以让它变成一个类的静态功能,所以你可以这样调用:node::hashString(s)如果你想保持它与节点类相关联。