CreasyBear
CreasyBear

Reputation: 65

Trouble with C++ maps and their content

This is rather extensive, so in advance, if you get through this, even without an answer or solution, thank you.

So, I have this program that is meant to be a basic social network, minus the user-interface, in which a user is represented by a Person object, which is responsible for maintaining a friend list, a block list, a list of messages, and a queue of pending friend requests. These lists are, respectively, of types std::map<std::string, Person>, std::map<std::string, Person>, std::vector<Message>, and std::queue<Message>, where the std::string for the two maps is a concatenation of the user's first and last names and the Message in the final two containers are and additional class I have defined. I have overloaded the << operator for Person such that it prints the user's first and last names, with a space in between. For some reason, when I go to print these names out, the return is empty, and I have no idea why. The following code is essentially a walkthrough what is happening.

The lines I am using to test my code in the main class:

std::string cFirst ("Chris");
std::string cLast ("Cringle");
SocialNetwork sn;
sn.addUser(cFirst,cLast);

The addUser() function in SocialNetwork:

void SocialNetwork::addUser(std::string first, std::string last){
std::string name = (first + last);
Person user (first, last);
_users.insert(std::pair<std::string, Person>(name, user));
}

Where _users is member data on the SocialNetwork of type std::map<std::string, Person>. The constructor for Person is:

Person::Person(std::string first, std::string last){
_first = first;
_last = last;
}

Where _first and _last are member data on Person that represent the user's first and last names. Then, back in the main class, after sn.addUser(cFirst,cLast);:

sn.printUsers();

Which looks like:

void SocialNetwork::printUsers(){
std::map<std::string, Person>::iterator it;
it = _users.begin();
while(it != _users.end()){
    cout << it->first << endl;
    cout << it->second << endl;
    it++;
    }
}

With the given code I have, the expected output for cout << it->first << endl; should be ChrisCringle, and it is. The expected output for cout << it->second << endl; should call the overloaded operator and should be Chris Cringle, but it simply prints a blank space. Any indications as to why would be greatly appreciated. Do I need to pass my params by reference? I have tried this already and seem to run into a lot of trouble. If something appears to be missing that may help, feel free to ask! Thanks again! I know I'll probably get a lot of flak for this long question but I do not think I can manage to make this any more of a simple question.

EDIT: The code for the overloaded operator is:

ostream& operator<<(ostream& os, const Person& per){
os << per._first << " " << per._last;
return os;
}

Upvotes: 3

Views: 90

Answers (1)

RiaD
RiaD

Reputation: 47619

I just used all the code you've shown: http://ideone.com/mFBxTC

#include <string>
#include <iostream>
#include <map>
using namespace std;

struct Person {
    Person(std::string first, std::string last);
    std::string _first, _last;
};


ostream& operator<<(ostream& os, const Person& per){
    os << per._first << " " << per._last;
    return os;
}


struct SocialNetwork {
    void addUser(std::string first, std::string last);
    std::map<std::string, Person> _users;
    void printUsers();
};


void SocialNetwork::addUser(std::string first, std::string last){
  std::string name = (first + last);
  Person user (first, last);
  _users.insert(std::pair<std::string, Person>(name, user));
}

Person::Person(std::string first, std::string last){
  _first = first;
  _last = last;
}

void SocialNetwork::printUsers(){
std::map<std::string, Person>::iterator it;
it = _users.begin();
while(it != _users.end()){
        cout << it->first << endl;
        cout << it->second << endl;
        it++;
    }
}


int main() {
    std::string cFirst ("Chris");
    std::string cLast ("Cringle");
    SocialNetwork sn;
    sn.addUser(cFirst,cLast);
    sn.printUsers();
    return 0;
}

And it works fine. So error is elsewhere

Thats why one should post SSCCE when posting debugging questions.

Upvotes: 2

Related Questions