0

I have a program for school to make and i thought i'd use vectors, everything went smoothly but at the end I saw that function in my program that pushes elements to vector of classes do it only inside function, out of function the vector has the same size.

I wrote a small test code to show it.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cassert>

using namespace std;

class Player {
public:
    int i = 1;

    Player() {}
};

void pb(vector<Player> v) {
    v.push_back(Player());
    cout << v.size() << endl;
}

int main()
{
    vector<Player> v;
    v.push_back(Player());

    cout << "1st element i = " << v[0].i << endl;
    cout << "vector size: " << v.size() << endl;

    pb(v);

    cout << "Second element i = " << v[1].i << endl;
    cout << "vector size: " << v.size() << endl;
}

The code send error "Vector subscript out of range" because I try to enter the non-existent v[1] that should exist because in function I did use push_back().

Can someone explain to me why does it happen, and how would I do it with the usage of pointers if I can do so?

Lubersky
  • 1
  • 3
  • by any chance are you coming from a java background? In any case, C and C++ are two different languages and Java is completely different – 463035818_is_not_an_ai Apr 08 '20 at 13:44
  • Never wrote in java. – Lubersky Apr 08 '20 at 13:46
  • ok, doesnt really matter. C++ has value semantics, in a nutshell: if you dont ask for a reference what you get is a copy – 463035818_is_not_an_ai Apr 08 '20 at 13:46
  • Got it, gotta read more about reference and how all this work, i write in c++ quite a bit time but always had problem with pointers, references and how all this work inside out all the time. Thanks! – Lubersky Apr 08 '20 at 13:49
  • If you passed an `int` to a function and changed it inside the function would you expect that change to be visible outside? Why should it be any different with a vector? – john Apr 08 '20 at 13:49
  • @john try to tell that to a java fan ;) – 463035818_is_not_an_ai Apr 08 '20 at 13:51
  • @john Every time I did that, I always returned that value, so never really expected the kind of problem like that, if it was on a int i would propably just solve the problem myself. – Lubersky Apr 08 '20 at 13:53
  • @idclev463035818 Of course it's one of C++'s strengths that it treats primitive types and objects in a consistent way. But for some reason (and not just Java) the expectation is often that they will not be treated the same. – john Apr 08 '20 at 13:54
  • @Lubersky Nothing stops you returning the vector from the function, that's a reasonable alternative to using a reference. – john Apr 08 '20 at 13:56
  • I think that a lot of the confusion is that a `std::vector` looks more complex than an `int`, thus the new programmer believes that the rules of C++ changes if you pass / return an `int` or a `vector`. A type is a type, whether it is a simple type like an `int`, `double`, etc. or a more complex-looking type like a `std::vector`. – PaulMcKenzie Apr 08 '20 at 14:17
  • @PaulMcKenzie Exactly! :) – Lubersky Apr 08 '20 at 19:30

1 Answers1

3

You are taking the vector by copy. The pb function needs to take the vector by reference to see the changes at the call site

void pb(vector<Player> &v) {
    v.push_back(Player());
    cout << v.size() << endl;
}
cigien
  • 57,834
  • 11
  • 73
  • 112