Home > front end >  push_back of an integer doesn't work on my vector of strings
push_back of an integer doesn't work on my vector of strings

Time:03-31

i am trying to push back 3 vectors in parallel and when i get to push_back the string vector i get the error of :

"no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::string, _Alloc=std::allocatorstd::string]" matches the argument listC/C (304) ask3.cpp(38, 8): argument types are: (int) ask3.cpp(38, 8): object type is: std::vector<std::string, std::allocatorstd::string>"

here is the codechunk that im on :

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    int length, count = 0, moviecount = 0, spacecount = 0;
    ;
    vector<int> status, price;
    vector<string> movies;
    string FileName, text, line, dummy;

    FileName = "MovieList.txt";

    ifstream InFile;
    InFile.open(FileName);
    while (!InFile.eof()) {
        getline(InFile, line);
        text  = line   "\n";
    }
    cout << text;

    length = text.length();
    for (int i = 0; i <= length; i  ) {
        if (text[i] == ' ') {
            spacecount  ;
        }
    }
    if (spacecount == 2) {
        moviecount = 1;
    }
    else if (spacecount > 2) {
        int temp = spacecount;
        temp = temp - 2;
        temp = temp / 3;
        moviecount = 1   temp;
    }
    movies.push_back(moviecount); //<-- problem line
    status.push_back(moviecount);
    price.push_back(moviecount);
}

CodePudding user response:

movies is a vector of string, so you cannot push int directly.

If you are using C 11 or later, you can use std::to_string to convert integers to strings.

Another way to convert integers to strings is using std::stringstream like this:

std::stringstream ss;
ss << moviecount;
movies.push_back(ss.str());
  • Related