gpt4 book ai didi

c++ - 使用 C++ 读取标准输入并连接内部字符串

转载 作者:行者123 更新时间:2023-11-30 03:28:12 25 4
gpt4 key购买 nike

<分区>

我想让我的应用程序接受一个参数或从标准输入读取,并将其打印在屏幕上。但是我在尝试连接字符串时遇到了问题。

我发现“+”可以,但它给我带来了问题,所以我尝试使用 append 方法,该方法现在抛出了我在下面显示的错误。

#include <iostream>
using namespace std;

std::string Read_stdin(){
std::string result="";
std::string input_line="";
while(cin) {
getline(cin, input_line);
result.append(input_line).append(endl);
};
}


int main(int argc, char** argv)
{
std::string input = (argc == 2) ? argv[1] : Read_stdin();
cout << "Your input is : " << input;
return 0;
}

输入:

enter image description here

错误:

jdoodle.cpp: In function 'std::__cxx11::string Read_stdin()':
jdoodle.cpp:13:46: error: no matching function for call to 'std::__cxx11::basic_string<char>::append(<unresolved overloaded function type>)'
result.append(input_line).append(endl);
^
In file included from /usr/include/c++/5.3.0/string:52:0,
from /usr/include/c++/5.3.0/bits/locale_classes.h:40,
from /usr/include/c++/5.3.0/bits/ios_base.h:41,
from /usr/include/c++/5.3.0/ios:42,
from /usr/include/c++/5.3.0/ostream:38,
from /usr/include/c++/5.3.0/iostream:39,
from jdoodle.cpp:1:
/usr/include/c++/5.3.0/bits/basic_string.h:983:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::append(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
append(const basic_string& __str)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com