错误:'words'前缺少模板参数

error: missing template arguments before 'words'

本文关键字:参数 words 错误      更新时间:2023-10-16

这是一个程序,它从用户那里读取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;
    };
};

名称hashstd::hash冲突。

为了纠正这一点,

  1. 停止使用using namespace std;
  2. 命名空间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;
}