2015-10-26 31 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)如果你想保持它與節點類相關聯。