-2

I am trying to push back 3 vectors in parallel, and when I get to push_back() into the string vector, I get this error:

no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::string, _Alloc=std::allocator<std::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::allocator<std::string>>

Here is the chunk of code that I'm in:

#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);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dami Karv
  • 1
  • 6
  • 3
    Side note: your usage of `while(!InFile.eof())` is [wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). You should check if reading succeeded *before* using what are "read". – MikeCAT Mar 31 '22 at 14:43
  • 3
    `moviecount` is an `int`. `movies` is a `vector`. There's no implicit conversion from an `int` to `std::string`. It's not clear what you want to be happening here, but that's why it's refusing to compile. – Nathan Pierson Mar 31 '22 at 14:45
  • @Dami Karv What is unclear with the error message?! – Vlad from Moscow Mar 31 '22 at 14:57
  • Aside: why do you need 3 vectors that each contain the same 1 `int`? What are you trying to do with this program? – Caleth Mar 31 '22 at 15:47

2 Answers2

2

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());
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks, it worked. I hope this question can be useful because googling it directly doesn't get many results. – Dami Karv Mar 31 '22 at 14:49
  • "*Another way to convert integers to strings is ...*" - `std::ostringstream` would be more appropriate in that case. – Remy Lebeau Mar 31 '22 at 18:46
0

This is because you are trying to push int values in a vector movies that is initialized to store string values.

Not sure what you are trying to get out of this code but You can either change the vector's type to int like so : vector<int> movies or convert int to string using std::to_string() before push_back() like so : movies.push_back(std::to_string(moviecount))