C++ getline() 错误,没有与参数列表匹配的重载函数"getline"实例

c++ getline() error, no instance of overload function "getline" matches the argument list

本文关键字:getline 函数 实例 列表 重载 错误 C++ 参数      更新时间:2023-10-16

使用getline()函数时,我收到以下错误:

没有重载函数"getline"的实例与参数列表匹配

在一个名为"时间"的类中,我在阅读以下输入时使用它:

istream & operator >> (istream & input, Time & C) /// An input stream for the hour     minutes and seconds
{
char ch;
input >> C.hour >> ch >> C.minute >> ch >> C.second;
getline(input,C.ampm,',');
return input;   /// Returning the input value
}

这很好用,但我也想把它用于另一个名为"股票"的类:

istream & operator >> (istream & input, Shares & C) /// An input stream for the day, month and year
{
char ch;
input >> C.price >> ch >> C.volume >> ch >> C.value >> ch;
getline(input,C.value,',');
return input;   /// Returning the input value
}

然而,"shares"类中的getline函数给了我错误。两个类都在使用库:

#include <iostream>
#include <string>

我该如何克服这一点?感谢

getline(input,C.value,',');

根据这些评论,您写道C.value是双的。这不会成功,因为正如其他人所指出的,预期的参数是其中的字符串类型。

您需要读入一个临时字符串,然后将其转换为双精度字符串。后一个步骤很简单,但使用C++11的std::stod更简单。

因此,你会写这样的东西:

std::string valueString;
getline(input, valueString, ',');
C.value = std::stod(valueString);