在main()中使用getline()函数的名称空间错误

时间:2019-02-07 06:16:33

标签: c++ namespaces

我在编译此代码时遇到错误。我在顶部包含了名称空间,但仍然让我感到困惑。 (PS我是这个名称空间的新手)

#include <iostream>
#include <string>
using namespace std;


#define MAX_HASH_CODE   10000

/* Function prototypes */

int Hash(string s, int maxCode);

#define Multiplier -1664117991L     // Multiplier used in Hash function

int Hash(string s, int maxCode)
{
    unsigned long hashcode = 0;
    for (int i = 0; i < s.length(); i++) 
        hashcode = hashcode * Multiplier + s[i];
   return (hashcode % maxCode);
}

int main ()
{
    cout << "Please enter your name: ";
    string name = getLine();

    int hashcode = Hash(name, MAX_HASH_CODE);
    cout << "The hash code for your name is " << hashcode << "." <<endl;

    return 0;
}

错误:在此范围内未声明“ getLine”   字符串名称= getLine();

1 个答案:

答案 0 :(得分:3)

应该是这样的:

int main ()
{
    string name;
    cout << "Please enter your name: ";
    getline(cin, name); // As previously stated, getline() is case sensitive and requires arguments - like this.

    int hashcode = Hash(name, MAX_HASH_CODE);
    cout << "The hash code for your name is " << hashcode << "." <<endl;

    return 0;
}