Sunday, January 9, 2022

[SOLVED] error: no matching function for call to ‘std::vector<std:

Issue

I write func to write strings from file to vector and have a error

error : error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(std::ifstream&)

code:

//parse file file

#include <fstream>
#include <vector>

const unsigned short numOfStrings=5;

void ReadFromFile (std::ifstream& thisin, std::vector<std::string>& thisData)
{
    thisData.push_back(thisin);
}

int main ()
{
}

Solution

You are trying to push into the vector the ifstream, not reading from it.

To read from ifstream, you could have done the following.

void ReadFromFile (std::ifstream& thisin, std::vector<std::string>& thisData)
{
    std::string word;
    while(thisin >> word)
    {
        thisData.push_back(word);
    } 
}


Answered By - Karen Baghdasaryan