0

So I have a vector that I want to modify, and a function that should do so. But when I output the values of the vector after calling the function, nothing has changed.

Here is my vector:

vector<char> board = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};

And here is my function:

void take_turn(vector<char> board) {
      int input;
      cin >> input;
      board[input] = 'x';
    }

Let's say the input is 1. Here is what I was expecting:

  , x,  ,  ,  ,  ,  ,  ,  ,


Here is what I get:

 ,  ,  ,  ,  ,  ,  ,  ,  ,
  • 1
    Take it by reference: `void take_turn(vector& board)` or else you'll only modify the local copy inside the function. – Ted Lyngmo Mar 15 '23 at 17:08
  • 1
    By passing it by reference. [Does C++ pass objects by value or reference?](https://stackoverflow.com/questions/21215409/does-c-pass-objects-by-value-or-reference) – Jason Mar 15 '23 at 17:09

0 Answers0