c++ - 初学者错误 "getline"

标签 c++ getline

这是我第一次发布问题,所以我希望我做对了。无论如何,我正在尝试创建一个程序来询问用户一个字符串,计算字母的类型和数量,然后输出字母的频率。到目前为止,我什至在获得正确的输入时都遇到了错误,只是无法弄清楚问题是什么。我的(相关)代码是:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;    

string getPhrase(const string & phrase);  //Function for gathering string input
int main()
{
     const string phrase;
     getPhrase(phrase);
     ...
}

string getPhrase(const string &phrase)
{
   cout<<"Enter phrase: "
   getline(cin, phrase);

   return (phrase);
}

当我运行时,出现以下错误:

freq.cpp: In function ‘std::string getPhrase(const std::string&)’:
freq.cpp:21: error: no matching function for call to ‘getline(std::istream&, const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’

我不知道自己做错了什么,而且似乎无法在网上找到与我正在做的事情相关的任何内容。

最佳答案

您的 getPhrase 应该如下所示:

std::string getPhrase()
{
    std::string result;
    std::cout << "Enter phrase: ";
    std::getline(std::cin, result);
    return result;
}

然后:

int main()
{
    std::string phrase = getPhrase();

    // ...
}

关于c++ - 初学者错误 "getline",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913808/

相关文章:

c++ - 如何在 opencv 中为 3 维数组赋值

c++ - 通过接口(interface)测试实现

c++ - 在Qt中有什么快速触发信号槽的好方法吗?

c++ - Unix 函数 gmtime_r 的 Windows 等价物是什么?

C getline() 在 EOF 之前返回 -1

c - 使用 sscanf 迭代

c# - 要在 C# 中使用的 PInvoke C++ 函数

c++ - 为什么要在空格中插入数字和特殊字符?

c++ - C++ 中的 getline(isstream, string)

c++ - getline() 函数从何而来?