从有效文件读取时,没有对应的错误调用函数

No matching function for call to error when reading from a valid file

本文关键字:错误 函数 调用 文件 有效 读取      更新时间:2023-10-16

我在一个文件中有以下数据:

0001 O 100 102.30
0001 O 101 333.22
0001 O 102 679.13
0001 P 103 513.36
0001 P 104 700.94

,代码如下:

vector<string> customerID;
vector<char> transactionType;
vector<string> transactionNumber;
vector<double> amount;
string cID, tT, tN, amnt;
for(;infile2 >> cID >> tT >> tN >> amnt;){
    customerID.push_back(cID);
    transactionType.push_back(tT);
    transactionNumber.push_back(tN);
    amount.push_back(amnt);
}

和错误:

error: no matching function for call to 'std::vector<char>::push_back(std::string&)'
error: no matching function for call to 'std::vector<double>::push_back(std::string&)'

是否假设每个数据项都是字符串?

是的。你正在读取四个字符串,你正在将它们中的任何一个转换成其他任何东西。c++不提供从std::string到非字符串类型的隐式转换。

解决问题的最简单方法是将tTamnt读取为chardouble。只需将变量声明为

std::string cID, tN;
char tT;
double amnt;

,它应该可以工作。或者,您可以将它们作为字符串读取并转换。

如上所述,您在读取时使用了四个string变量。您可以通过声明适当类型的变量来纠正它。

vector<string> customerID;
vector<char> transactionType;
vector<string> transactionNumber;
vector<double> amount;
string cID, tN;
char tT;
double amnt;
for(;infile2 >> cID >> tT >> tN >> amnt;){
    customerID.push_back(cID);
    transactionType.push_back(tT);
    transactionNumber.push_back(tN);
    amount.push_back(amnt);
}