简体   繁体   English

cin.getline : 没有匹配的调用函数

[英]cin.getline : no matching function for call

In many answers and questions such as this one, it is recommended to use cin.getline from <string> and not getline(cin, variable) :在许多答案和诸如疑问一个,则建议使用cin.getline<string>而不是getline(cin, variable)

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;
    cin.getline(name);
}

But in my case I have build issue:但就我而言,我有构建问题:

g++ foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:8:21: error: no matching function for call to 
   ‘std::basic_istream<char>::getline(std::string&)’
    8 |     cin.getline(name);
      |                     ^

From the error told by the compiler, it is evident that there is no such function overload that accepts a type reference to std::string (for cin.getline() ).从编译器告诉的错误中,很明显没有这样的函数重载接受std::string的类型引用(对于cin.getline() )。 Rather, it accepts parameters like:相反,它接受如下参数:

const int MAX = 100;

char input[MAX];
cin.getline(input, MAX);
// Or better
// cin.getline(input, sizeof input);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM