2016-03-03 69 views
1

這是一個程序,它從用戶讀取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; 
    }; 
}; 

更新:我modifyed如下面的代碼,但仍得到相同的錯誤。

#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

當您在main中聲明散列時,未定義散列。你需要轉發宣佈這個類。 –

回答

0

名稱hashstd::hash衝突。使用using namespace std;

  • 添加std::每個標識符從命名空間std或添加像using std::cin;每個標識符,而不是using namespace std;
  • UPDATE聲明

    爲了解決這個問題,

    1. 停止:你代碼應該是這樣的:

      #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; 
      } 
      
    +0

    我不確定我完全理解。我根據我的解釋編輯了代碼。這是足夠的還是我仍然錯過了什麼? – OxNightRo

    +0

    @OxNightRo Rollbacked不會打破這個問題。這是不夠的。 – MikeCAT

    +0

    啊,好吧,我現在明白了。我做了修改並運行了程序。但是,它排除了第11行中的第一個錯誤。但是,我仍然得到了與之前存在的錯誤相同的錯誤。 – OxNightRo