我的编译器不允许我使用 getline

My compiler wont let me use getline

本文关键字:getline 允许我 编译器 不允许 我的      更新时间:2023-10-16

当我尝试访问getline时,它会给我这样的错误!我想做的只是创建一个单独的简单文本。

C:\c++3+\Strings\main.cpp|11|error: 没有匹配函数调用 'getline(std::istream&, std::string&, const char [2])'|

   #include <iostream>
#include <string>
using namespace std;
int main()
{
    string last,name,playerclass;

   getline(cin, last, ",");
   getline(cin, name, ",");
   getline(cin, playerclass, ",");
   cout << name << last << " Is a " << playerclass;
    return 0;
}

getline将单个字符作为分隔符的最后一个参数。你必须使用

std::getline( cin, last, ',' );

' 代替 "

编译器的错误:

no matching function for call to 'getline(std::istream&, std::string&, const char [2])

它告诉您编译器试图找到一个将最后一个参数作为字符串(大小为 2 的文字字符串)的相应函数,但失败了。