0

I'm working on a chatroom program, and I am trying to add a user to a chatroom map. My chatroom map is stored in my Server class and it looks like this: map<Chatroom*,int> chatrooms where int is the number of users in the chatroom. Also in my Server class is a vector of all the users currently in the server:

vector<User*> current_users. server.getUsers() returns the current_users and server.get_chatrooms() returns the map chatrooms. My function properly adds the user to the chatroom, however, it doesn't increment the number of users in the chatroom. I wrote a comment where the problem is.

Here is the function.

void Controller::add_user_to_chatroom(){
    string username, chatroom_name;
    User* user;
    bool foundChat = false;
    bool foundUser = false;

    view.username_prompt();
    cin >> username;

    //this loops checks to see if user is on the server 
    for(auto x : server.get_users()){
        if(x->getUsername() == username){
            user = x;
            foundUser = true;
            break;
        }
    }

    if(!foundUser){
        cout << "No user found.\n" << endl;
    }
    else{
        view.chatroom_name_prompt();
        cin >> chatroom_name;

        //adds user to chatroom, but doesn't increment the number
        for(auto x : server.get_chatrooms()){
            if(x.first->get_name() == chatroom_name){
                x.first->add_user(user);

                //line below doesn't work, tried x.second++;
                server.get_chatrooms().at(x.first) += 1;
                foundChat = true;
                break;
            }
        }

        if(!foundChat){
            cout << "Chatroom not found.\n" << endl;
        }
    }
}

My output when there I print chatrooms looks like this: Chatroom Name: Sports, Users: joey1212, , Num Users: 0

However, it should look like this: Chatroom Name: Sports, Users: joey1212, , Num Users: 1 since there is one user in the chatroom.

Why is x.second not updating? I've added several users to the same chatroom and num users never updates. Just in case it's needed, here are the other functions that were called from add_user_to_chatroom()

Here's Server::get_users()

vector<User*> Server::get_users(){
    return users;
}

Here's Server::get_chatrooms()

 map<Chatroom*, int> Server::get_chatrooms(){
    return chatrooms;
 }
weston
  • 54,145
  • 21
  • 145
  • 203
rbb091020
  • 37
  • 1
  • 1
  • 10
  • 1
    Think you need auto& https://stackoverflow.com/a/29860056/360211 or you're modifying a local copy of the map entry, not the map entry. – weston Apr 01 '19 at 04:17

1 Answers1

1

get_chatrooms returns a copy of the map. When you try to change the number of users in a room, you're changing the value in the copy and not the one in server.chatrooms.

Change get_chatrooms to return a reference:

map<Chatroom*, int> &Server::get_chatrooms()
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • Now that I think about it... why does it work for get_users() if it's only getting a copy of the users and not the reference? What is the difference between get_users() and get_chatrooms() for the different behavior? – rbb091020 Apr 03 '19 at 01:47
  • @rbb091020 Since `get_users` returns a `vector`, it doesn't matter if you use a copy or a reference to the original - you get the same user pointers. `get_chatrooms` returns a map, and when you iterate thru that you get the same `x.first` (because both pointers have the same value), but `x.second` refers to different ints. – 1201ProgramAlarm Apr 03 '19 at 03:52