1

I tried to use vectors instead of arrays in one of the parts of my program, because of other problems. I have never really used them previously.

This is the part of the code:

#include <vector>

ifstream file("data.txt");
vector<int> beds;
vector<int> people;
vector<int> balconies;

while(!file.eof()){
    file >> people.push_back();
    file >> beds.push_back();
    file >> balconies.push_back();
}

I have no idea if it would be working. Anyway now I have an error: No matching member function for call to 'push_back'.

  • 1
    You can't learn C++ by trial and error, or throwing bits of code together without understanding them. Where are you learning this? – BessieTheCookie Mar 31 '20 at 18:50
  • 1
    Read the documentation for the proper use of `push_back`. https://en.cppreference.com/w/cpp/container/vector/push_back – JohnFilleau Mar 31 '20 at 18:51

2 Answers2

3

The method std::vector::push_back accepts an argument, which is the value to add to the end of the vector. So you need to break each of those calls into two steps: first read the value into an int, then push_back that value into the vector.

while(!file.eof()){
    int temp;
    file >> temp;
    people.push_back(temp);
    file >> temp;
    beds.push_back(temp);
    file >> temp;
    balconies.push_back(temp);
}

As mentioned in the comments, I would suggest against your while condition as written. This post explains why in detail, along with providing better alternatives.

Cory Kramer
  • 98,167
  • 13
  • 130
  • 181
1

Store input data inside a variable before and then push that variable

int example;
file >> example;
people.push_back(example);

or use std::istream_iterator

dcariotti
  • 410
  • 1
  • 3
  • 10