function not returning values in vector

I have a class that has a function that should return a vector. The code runs correctly but nothing is returned. When I check the returned vector size it is zero. When I check the size of the vector in the class it has 11 items. I am new to c++ but have used java. Any help would be appreciated. thank in advance.


class Person
{
string name; //private variables for this class
vector<string> personpref;
public:
//public methods that this class will have
Person::Person()
{}
void setName(string n)
{
name=n;
}
string getName()
{
return name;
}
vector<string> getPersonPref()
{
return personpref;
}
};

Code snippet that should get the vector. Person csuitor.getName() is returning the name from the class csuitor.getPersonPref() is returning an empty vector.

while (!cbachelorlist.empty())//iterate over the bachelors list
{
Person csuitor = cbachelorlist.front();
cout <<"csuitor "<< csuitor.getName() <<"\n";
vector<string> cpref=csuitor.getPersonPref();
std::cout <<"pref size" <<cpref.size() <<"\n";
//iterator over his preference
for (vector<string>:: const_iterator it=cpref.begin(); it !=cpref.end();++it)
I think that the error somewhere else. This code snip is not enough to make conclusion.
OK, I am posting the entire code.

class Person
{
string name; //private variables for this class
vector<string> personpref;
public:
//public methods that this class will have
Person::Person()
{}
void setName(string n)
{
name=n;
}
string getName()
{
return name;
}
vector<string>& getPersonPref()
{
return personpref;
}
void addToList(string n)
{//will add a bride to preflist of groom
personpref.push_back(n);
//std::cout <<"add to list "<<n<<"\n";
std::cout << "person pref in class size " <<personpref.size() << "\n";
}
bool isNameInList(string n)
{
//iterate over groompref

for (vector<string>::iterator it=personpref.begin();it!=personpref.end();++it)
{
if(*it==n)
{
return true;
}
return false;
}
}
bool cPrefers(string first, string second)
{
for (vector<string>::iterator it=personpref.begin();it!=personpref.end();++it)
{
if(*it==first) return true;
if (*it==second)return false;
}
return false;
}


};
//get the preferencelist, first preference, second preference












const char *men_data[][11] = {
{ "abe", "abi","eve","cath","ivy","jan","dee","fay","bea","hope","gay" },
{ "bob", "cath","hope","abi","dee","eve","fay","bea","jan","ivy","gay" },
{ "col", "hope","eve","abi","dee","bea","fay","ivy","gay","cath","jan" },
{ "dan", "ivy","fay","dee","gay","hope","eve","jan","bea","cath","abi" },
{ "ed", "jan","dee","bea","cath","fay","eve","abi","ivy","hope","gay" },
{ "fred", "bea","abi","dee","gay","eve","ivy","cath","jan","hope","fay" },
{ "gav", "gay","eve","ivy","bea","cath","abi","dee","hope","jan","fay" },
{ "hal", "abi","eve","hope","fay","ivy","cath","jan","bea","gay","dee" },
{ "ian", "hope","cath","dee","gay","bea","abi","fay","ivy","jan","eve" },
{ "jon", "abi","fay","jan","gay","eve","bea","dee","cath","ivy","hope" }
};

const char *women_data[][11] = {
{ "abi", "bob","fred","jon","gav","ian","abe","dan","ed","col","hal" },
{ "bea", "bob","abe","col","fred","gav","dan","ian","ed","jon","hal" },
{ "cath", "fred","bob","ed","gav","hal","col","ian","abe","dan","jon" },
{ "dee", "fred","jon","col","abe","ian","hal","gav","dan","bob","ed" },
{ "eve", "jon","hal","fred","dan","abe","gav","col","ed","ian","bob" },
{ "fay", "bob","abe","ed","ian","jon","dan","fred","gav","col","hal" },
{ "gay", "jon","gav","hal","fred","bob","abe","col","ed","dan","ian" },
{ "hope", "gav","jon","bob","abe","ian","dan","hal","ed","col","fred" },
{ "ivy", "ian","col","hal","gav","fred","bob","abe","ed","jon","dan" },
{ "jan", "ed","hal","gav","abe","bob","jon","col","ian","fred","dan" }
};

typedef map<string, Person> CPersonlist;
typedef map<string, string> CCouples;

int main()
{
//my code starts here----
//init data structures
CPersonlist cbachelors;
CPersonlist cbachelorettes;
queue<Person>cbachelorlist;
for (int i = 0; i < 10; ++i) // person
{
//make a groom
Person g=Person();
g.setName(men_data[i][0]);
//make a bride
Person b=Person();
b.setName(women_data[i][0]);

cbachelors[men_data[i][0]]=g; //add to bachelors list
cbachelorlist.push(g); //list of bachelors that we will use to iterate
cbachelorettes[women_data[i][0]]=b;//add to bachelorettes list
}

//get their preferences
string boyname;
string girlname;


for (int i = 0; i < 10; ++i) // person
{
//make a groom
Person g=Person();
g.setName(men_data[i][0]);
//make a bride
//std::cout << "print groom name" <<g.getName() <<"\n";
Person b=Person();
b.setName(women_data[i][0]);

cbachelors[men_data[i][0]]=g; //add to bachelors list
cbachelorlist.push(g); //list of bachelors that we will use to iterate
cbachelorettes[women_data[i][0]]=b;//add to bachelorettes list
for (int j = 1; j < 11; ++j) // preference
{
g.addToList(women_data[i][j]);
b.addToList(men_data[i][j]);

}
}

CCouples cengaged;
std::cout<<"Class matchmaking:\n";
while (!cbachelorlist.empty())//iterate over the bachelors list
{
Person &csuitor = cbachelorlist.front();
cout <<"csuitor "<< csuitor.getName() <<"\n";
vector<string> &cpref=csuitor.getPersonPref();
std::cout <<"pref size" <<cpref.size() <<"\n";
//iterator over his preference
for (vector<string>:: const_iterator it=cpref.begin(); it !=cpref.end();++it)
{
//can't get this to print out
cout <<"cpref "<<*it<<"\n";
Person cbride=cbachelorettes[*it];//it is a string so find person in bachelorettes list that matches the string it, return object Person
std::cout << "print name " <<cbride.getName() <<"\n";
if (cengaged.find(cbride.getName())==cengaged.end()) //look in cengaged, if she is not in there then she is availabe.
{
std::cout << "\t" << cbride.getName() << "and " << csuitor.getName() << "\n";
cengaged[cbride.getName()]=csuitor.getName(); //hook up
break;
}
string cgroom=cengaged[cbride.getName()];


if (cbride.cPrefers(csuitor.getName(),cgroom))
{
std::cout << "\t" << cbride.getName() << "dumped " << cgroom << " for " << csuitor.getName() << "\n";
//get object that matches the name of groom from cbachelors
Person cgroompush=cbachelors[cgroom];
cbachelorlist.push(cgroompush); // dump that zero
cengaged[cbride.getName()] = csuitor.getName(); // get a hero
break;
}
}
cbachelorlist.pop(); // pop at the end to not invalidate suitor reference
}
}
> I am new to c++ but have used java.

C++ makes a distinction between values, references, and pointers (pointers are the closest to references in Java).

In Person::getPersonPref() returns a vector by value; a copy of the personpref member is made and returned. After that, if elements are added to the personpref member, the copy will not be affected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <vector>
#include <iostream>

struct A
{
    std::vector<int> seq ;

    std::vector<int> get_copy_of_seq() const { return seq ; }

    std::vector<int>& get_reference_to_seq() { return seq ; }
};

int main()
{
    A a ;

    auto val = a.get_copy_of_seq() ;
    auto& ref = a.get_reference_to_seq() ;

    std::cout << val.size() << '\n' ; // 0
    std::cout << ref.size() << '\n' ; // 0

    a.seq.resize(1234) ;
    std::cout << val.size() << '\n' ; // 0
    std::cout << ref.size() << '\n' ; // 1234
}


http://ideone.com/rYuuNG
All the items were added before I called the function that returned the vector.This is what I can't understand. I want to get this working before I start looking at returning the vector as a reference.
> All the items were added before I called the function that returned the vector

Saw your complete code only now.

Change this
//vector<string> &cpref=csuitor.getPersonPref();

To
vector<string> cpref = csuitor.getPersonPref();

The object returned by csuitor.getPersonPref(); is an anonymous temporary.
Last edited on
I tried that and it did not work. I am still getting a vector with size 0. Thanks for your help. This has been frustrating me for the last few days. I will appreciate it if you can suggest something else.
Add this line to getPersonPref()

1
2
3
4
5
vector<string>& getPersonPref()
{
        std::cout << "getPersonPref() is returning a vector of size " <<  personpref.size() << '\n' ;
        return personpref;
}


What does it print out?
It is printing the size as zero.


Now I am totally confused because the the following is printing the correct sizes as I am adding the elements to the list

void addToList(string n)
{//will add a bride to preflist of groom
personpref.push_back(n);
//std::cout <<"add to list "<<n<<"\n";
std::cout << "person pref in class size " <<personpref.size() << "\n";
}

I am making each person, setting their name and preflist, adding them to the cbachelors queue
After that, I am getting a person from the cbachelors queue and trying to retrieve their name and preflist. I can retrieve the name but not the preflist. So it seems that the name is stored with my person object but not the preflist.
Don't know what to do......
Got it. Dumb Dumb mistake. I was adding the person to the bachelors list and then adding their preference. Thanks for all the help. You guys & gals are great!!!!!
Topic archived. No new replies allowed.