|
||
|
|
#1 (permalink) |
|
Forum Expert
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
|
In my program, I am trying to create a list of objects that contain a list of objects. I have a list of people, that each contain a list of surveys. Everything is fine, and I have no problem poplulating the lists using various functions, the problem is when I go to iterate and print out the properties. First let me mention that for each object, I have used a friend function to overload the extraction operator. Below is the function in which I am trying to print out each person in the personlist, and trying to access and print out each survey in the survey list (that the person object contains). Below is what I have tried with no luck. I created a function in the person.cpp that iterates through the list of surveys and requires a list<survey> as a parameter. It basically looks the same as the one below. Any ideas on how to go about this?
Code:
void EmployeeList::ViewEntries(list<Person> list)
{
for(iter = list.begin(); iter != list.end(); ++iter)
{
cout << (*iter) << endl;
//cout << (*iter).ViewSurveys((*iter).GetList()) << endl;
}
}
|
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
|
Actually, after a quick sandwich, soda, cigarette and bathroom break it occurred to me. In my viewsurveys function, I print out the objects (surveys) using the overloaded extraction operator, so all I have to do is call that function within the iteration of the personlist:
Code:
void EmployeeList::ViewEntries(list<Person> list)
{
for(iter = list.begin(); iter != list.end(); ++iter)
{
cout << (*iter) << endl;
(*iter).ViewSurveys((*iter).GetList());
}
}
EmployeeList.cpp Code:
#include "Controller.h"
#include "EmployeeList.h"
list<Person> personList;
list<Person>::iterator iter;
EmployeeList::EmployeeList(const string &f):filename(f)
{
cout << "Constructed a List" << endl;
Sleep(500);
}
EmployeeList::~EmployeeList()
{
// code to handle destruction
}
list<Person> EmployeeList::GetList()
{
return personList;
}
void EmployeeList::AddEntry()
{
string name;
int id;
cout << "Please enter your name: " << flush;
getline(cin, name);
cout << "Please enter your 5 digit empoyee I.D.: " << flush;
cin >> id;
personList.push_back( *(new Person(id, name)) );
}
void EmployeeList::ViewEntries(list<Person> list)
{
for(iter = list.begin(); iter != list.end(); ++iter)
{
cout << (*iter) << endl;
(*iter).ViewSurveys((*iter).GetList());
}
}
Code:
#include "Person.h"
#include "Controller.h"
Person::Person(int id, const string &n):name(n)
{
empId = id;
name = n;
GetSurveyInfo();
}
Person::~Person()
{
}
ostream& operator<< (ostream& osObject, const Person& person)
{
osObject << person.name << " | " << person.empId << endl << endl;
return osObject;
}
list<Survey> Person::GetList()
{
return surveyList;
}
void Person::GetSurveyInfo()
{
string name;
cout << "Enter the name of the survey: " << flush;
cin.ignore();
getline(cin, name);
Survey survey = *(new Survey(name));
cin >> survey;
AddSurvey(survey);
}
void Person::AddSurvey(Survey survey)
{
char ch;
system("cls");
cout << survey;
cout << "Is this information correct? (Y)es/(N)o?" << endl;
cin >> ch;
if (ch == 'N' || ch == 'n')
{
GetSurveyInfo();
}
else if(ch == 'Y' || ch == 'y')
{
surveyList.push_back( survey );
}
else
AddSurvey(survey);
Sleep(1000);
}
void Person::ViewSurveys(list<Survey> list)
{
for(iter = list.begin(); iter != list.end(); ++iter)
{
cout << (*iter) << endl;
}
}
Last edited by Axle; 01-06-2007 at 08:02 PM. |
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
actually what you do is what you need. however, this is better in OOP sense.
Code:
void Person::ViewSurveys()
{
for(iter = surveyList.begin(); iter != surveyList.end(); ++iter)
{
cout << (*iter) << endl;
}
}
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|