lundi 18 novembre 2019

No suitable user-defined conversion (Vectors)

I am trying to print the contents of a vector, but this is the error that I'm getting, I can't seem to understand what I'm doing wrong and how to fix it?

no suitable user-defined conversion from "std::vector<std::string, std::allocator<std::string>>" to "const std::vector<std::vector<std::string, std::allocator<std::string>>, std::allocator<std::vector<std::string, std::allocator<std::string>>>>" exists
    ifstream data("test.csv");
    string line;
    vector<vector<string>> parsedCSV;
    while (getline(data, line)) {
        stringstream lineStream(line);
        string cell;
        vector<string> parsedRow;
        while (getline(lineStream, cell, ','))
        {
            parsedRow.push_back(cell);
        }
        parsedCSV.push_back(parsedRow);

    }
    for (int i = 0; i < parsedCSV.size(); i++) {

        print(std::cout, parsedCSV[i]); // errors shows up here
    }

My print function looks like -

void print(std::ostream& out, vector<string> const& data) {
    std::copy(data.begin(), data.end(),
        std::ostream_iterator<string>(out, " "));
}

If I bundle the 'parsing CSV' code into a function like -

void parseCSV() {
    ifstream data("test.csv");
    string line;
    vector<vector<string>> parsedCSV;
    while (getline(data, line)) {
        stringstream lineStream(line);
        string cell;
        vector<string> parsedRow;
        while (getline(lineStream, cell, ','))
        {
            parsedRow.push_back(cell);
        }
        parsedCSV.push_back(parsedRow);

    }
    for (int i = 0; i < parsedCSV.size(); i++) {
        print(std::cout, parsedCSV[i]);
    }   
}

and call it in main, it works fine - which again, I don't understand why?

Aucun commentaire:

Enregistrer un commentaire