Linked Lists, Objects and Member Functions
Dec 27, 2012 at 11:01pm UTC
So I'm writing this simple practice program using a linked list where I create a list of employee objects which store their names, age, ect...
I was able to figure out how to create new objects (employees) using the linked list in order to enter the data but I'm having trouble printing out that data. I am able to enter data but nothing is printed out.
My question is how do I access member functions of a class through a linked list of objects (or even a vector while we're at it)?
My code isn't complete because I'm just trying to get it to work with first names before everything else.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <list>
#include <string>
using namespace std;
class emp_rec {
private :
string first_name, last_name;
int age, job_level;
public :
void set_first_name(string s) {first_name = s;}
void set_last_name(string s) {last_name = s;}
void set_age(int n) {age = n;}
void set_job_level(int n) {job_level = n;}
string get_first_name() {return first_name;}
string get_last_name() {return last_name;}
int get_age() {return age;}
int get_job_level() {return job_level;}
void print_first() {cout << first_name << endl;}
};
int main() {
list<emp_rec> EL;
list<emp_rec>::iterator iter;
string first, last;
int age;
while (true ) {
emp_rec* pt = new emp_rec;
EL.push_back(*pt);
cout << "Enter first name. Press ENTER to exit." << endl;
getline(cin, first);
if (first.size() == 0)
break ;
(*pt).set_first_name(first);
}
for (iter = EL.begin(); iter != EL.end(); iter++) {
cout << (*iter).get_first_name() << endl;
}
return 0;
}
I also get the same result by replacing
cout << (*iter).get_first_name() << endl;
with
(*iter).print_first();
Last edited on Dec 27, 2012 at 11:03pm UTC
Dec 27, 2012 at 11:13pm UTC
1 2 3 4
emp_rec* pt = new emp_rec;
EL.push_back(*pt);
//...
(*pt).set_first_name(first);
push_back() makes a copy, you are not setting the name of the object in the container.
Also, you've got a memory leak there. There is no point in that dynamic allocation.
Dec 27, 2012 at 11:29pm UTC
Well, if I get rid of the new keyword and just have
emp_rec* pt = emp_rec;
I get a "expected primary expression before ';' error".
So, I'm guessing i will have to use the list::insert function then.
Dec 28, 2012 at 12:01am UTC
1 2 3
emp_rec temp;
temp.set_first_name("foo" );
EL.push_back(temp);
Last edited on Dec 28, 2012 at 12:02am UTC
Dec 28, 2012 at 12:52am UTC
That works but now I've run into another problem which is that it stops letting me input something into the first name after creating one object so there is no way for me to end the program.
It worked with just the one variable like in the post above and it even worked when I set the variables myself like so...
1 2 3 4 5 6 7 8 9
emp_rec temp;
cout << "Enter first name." << endl;
getline(cin, first);
temp.set_first_name(first);
temp.set_last_name("Smith" );
temp.set_age(12);
EL.push_back(temp);
I am able to print out a list like I want.
First
Last
Age
First
Last
Age
.
.
.
But when I ask for user input...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
emp_rec temp;
cout << "Enter first name." << endl;
getline(cin, first);
if (first.length() == 0)
break ;
temp.set_first_name(first);
cout << "Enter last name." << endl;
getline(cin, last);
temp.set_last_name(last);
cout << "Enter age." << endl;
cin >> age;
temp.set_age(age);
EL.push_back(temp)
It gives me
1 2 3 4 5 6 7 8 9 10
Enter first name.
JOhn
Enter last name.
Smith
Enter age.
23
Enter first name.
JOhn
Smith
23
and won't let me enter another name.
Dec 28, 2012 at 1:34am UTC
This is your input
JOhn\nSmith\n23\n
when you do a
getline() the
'\n'
is discarded,
but that is not the case when you do
cin>>age;
So a
'\n'
remains in the buffer, and the program mistake it for the next name (an empty one)
You could simply
1 2
cin >> age;
cin.ignore();
Last edited on Dec 28, 2012 at 1:34am UTC
Dec 28, 2012 at 1:37am UTC
Cool, that did the trick!
Topic archived. No new replies allowed.