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);
}
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());