简体   繁体   中英

No matching overload function found Error

I am trying to return a vector with template T with data from a file to be searched through in another function.

The program I am working on is a program that stores the name, date of birth, and address that was inputted to the program. I have tried to store the returned vector in another vector with the template type but it keeps on showing:

Error C2672 'getDataToVector': no matching overloaded function found

Error C2783 'std::vector> getDataToVector(std::ifstream)': could not deduce template argument for '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;
}

I am a beginner in c++ programming and am very appreciative for any help anyone can give me.

The error says it cannot deduce what T in getDataToVector should be. It can either be deduced from arguments ( not applicable in your case) or you can set it explicitly: getDataToVector<std::string>(in); means T==std::string . In your case you want to pass on the T from searchData - > getDataToVector<T>(in);

But looking at your code there's no need for templates at all, line is always std::string so data.push_back(line); means that only std::vector<std::string> makes sense. Same for searchData as the T is reduntand and is not even part of the function signature.

For starters the function getDataToVector if it is not a member function of a class shall be declared before the function searchData because it is referred in the function.

Secondly the parameter of the function shall be a reference

template

vector<T> getDataToVector(std::ifstream &infile) {
                                       ^^^ 

As the function is a template function and can not deduce the template parameter then you should write

vector<T> file_data = getDataToVector<T>(in);
                                     ^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM