简体   繁体   English

错误:没有用于调用“getline”的匹配函数

[英]error: no matching function for call to 'getline'

#include<iostream>
#include<string>
using namespace std;
int main(){
    //initializing two string

     string s2,s1="hello";

     cout<<s2<<s1;

     cout<<endl;
     //transferring(copying) data from s1 to s2 
       getline(s1,s2);

     cout<<s2<<s1;
}

The error is caused by the fact that there is no getline that would accept two strings as parameters.该错误是由没有接受两个字符串作为参数的getline引起的。

To "transfer" or copy data from s1 to s2 you write要“传输”或复制数据从s1s2你写

s2 = s1;

It is unclear why you think you need getline .目前尚不清楚您为什么认为需要getline The method getline is for different purpose about which you can read eg here .方法getline用于不同的目的,您可以 在这里阅读。

You are using getline wrong, please take a look at: http://www.cplusplus.com/reference/string/string/getline/你用getline有错,请看: http : //www.cplusplus.com/reference/string/string/getline/

The getline function takes 2 parameters (a Istream and a string, instead of two times a string), that is the reason why you get the error. getline 函数采用 2 个参数(一个 Istream 和一个字符串,而不是两个字符串),这就是您收到错误的原因。

If you just want to copy a string, you can use the following code:如果只想复制一个字符串,可以使用以下代码:

std::string s1 = "myString";
std::string s2 = s1;

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

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