[英]No matching overload function found Error
我试图返回一个带有模板T的矢量,其中包含来自文件的数据,以便在另一个函数中进行搜索。
我正在使用的程序是存储输入到该程序的名称,出生日期和地址的程序。 我试图将返回的向量存储在具有模板类型的另一个向量中,但是它继续显示:
错误C2672'getDataToVector':找不到匹配的重载函数
错误C2783'std :: vector> getDataToVector(std :: ifstream)':无法推断出'T'的模板参数
template <class T>
void searchData(vector<string>& name, vector<int>& birthdate, vector<string>& address) {
bool found = false;
string entry;
string line;
int i = 0;
cout << "Please enter the name you want to search: " << endl;
getline(cin, entry);
std::ifstream in;
in.open("test_file.txt");
vector<T> file_data = getDataToVector(in);
while (!found) {
if (std::find(file_data.begin(), file_data.end(), entry) != file_data.end()) {
cout << "The name is found" << endl;
cout << file_data[i] << endl;
found = true;
}
i++;
}
}
template <class T>
vector<T> getDataToVector(std::ifstream infile) {
vector<T> data;
string line;
while (getline(infile, line)) {
data.push_back(line);
}
return data;
}
我是C ++编程的初学者,非常感谢任何人都能给我的帮助。
该错误表明它无法推断getDataToVector
T
应该是getDataToVector
。 可以从参数中推论得出(不适用于您的情况),也可以显式设置它: getDataToVector<std::string>(in);
表示T==std::string
。 你的情况,你想传递的T
从searchData
- > getDataToVector<T>(in);
但是看看您的代码根本不需要模板, line
始终是std::string
所以data.push_back(line);
意味着只有std::vector<std::string>
才有意义。 与searchData
相同,因为T
是redundand,甚至不是函数签名的一部分。
对于初学者,如果函数getDataToVector
不是类的成员函数,则应在函数searchData
之前声明它,因为它是在函数中引用的。
其次,该函数的参数应作为参考
模板
vector<T> getDataToVector(std::ifstream &infile) {
^^^
由于该函数是模板函数,无法推断出模板参数,因此您应该编写
vector<T> file_data = getDataToVector<T>(in);
^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.