I'm trying to determine what's going wrong with my code, and I have absolutely no idea what the problem is. I have an iterator that is returning the incorrect values for the object in a vector, even though printing that object in GDB shows the correct fields. The code inside the loop is not relevant for this particular issue. The three objects and functions I use in the below loop are
class User{
string password;
string username;
string getName() {return username}
string getPass() {return password}
};
class Room{
vector<User> users;
vector<Message> messages;
string name;
vector<User> getUsers() {return users}
vector<Message> getMessages() {return messages}
string getName() {return name}
};
class Message{
User sender;
string message;
User getSender() {return sender}
string getMessage() {return message}
};
The code in my loop is
vector<Room>::iterator roomIt;
vector<Message>::iterator messIt;
for(roomIt = rooms.begin(); roomIt < rooms.end(); roomIt++) {
for (messIt = roomIt->getMessages().begin(); messIt < roomIt->getMessages().end(); messIt++) {
.........
}
}
In gdb, when I use print roomIt->getMessages().begin()
I get
{sender = {password = "robin", username = "batman"}, message = "hello"}
However, when I use print messIt
or print *messIt
I get
{sender = {password = "batman", username = "hello"}, message = "robin"}
What could be causing this discrepancy? I'm completely lost as I can't see how printing the object is giving the correct output while setting messIt reference equal to that object alters the values.