gpt4 book ai didi

c++ - 获取线路 : template argument deduction/substitution failed

转载 作者:行者123 更新时间:2023-11-28 00:21:07 25 4
gpt4 key购买 nike

当我尝试逐行读取命令行的输出时出现此错误:

std::string exec(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "";

char buffer[128];
std::string result = "";

while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL) {
result += buffer;
}
}

pclose(pipe);

return result;
}

string commandStr = "echo Hello World";
const char *command = commandStr.c_str();

std::string output = exec(command);

std::string line;
while (std::getline(output, line)) {
send(sockfd, line.c_str(), line.length(), 0);
}

note: template argument deduction/substitution failed: note:
‘std::string {aka std::basic_string}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’ exec(command), line <- Error

最佳答案

要解决错误消息的原因,我猜它来自std::getline() . getline 需要 std::istream作为它的第一个参数。如果你想从 output 中读取,你可以使用 std::stringstream

std::stringstream ss(output);
while (std::getline(ss, line)) {
/* ... */
}

关于c++ - 获取线路 : template argument deduction/substitution failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27411011/

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