Printing vectors stored in another vector

I have a base class "Employee" that has two child classes "Manager" and "Salesperson". In the Employee class I have a method called addSalesperon( Salesperson * ) and a vector member variable vector<Salesperson *> staff_list where it stores employee objects. addSalesperon( Salesperson * ) just pushes the salesperson into the vector. Each employee also has another vector vector<Insurance *> sale_list; that stores Class objects from a class I create called "Insurance".

I have a print method that loops through the Employee objects in staff_list vector and prints the the insurgence objects stored in sales_list vector at each index. (Sorry I probably explained that poorly but if you look at the code you should be able to easily see what I was explaining)

The problem is when I try to call the print method nothing gets printed, but if I print the staff_list and sale_list vectors on their own they print fine. I'm just not sure whats wrong with my print method, and help clearing this up will be greatly appreciated!

Sample main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
list<Insurance *> sales_list; // List of all sales made by any employee
vector<Employee *> employee_list; // List of all existing employees

Employee *m = new Manager("Bob", "Smith", 62000);
Employee *s1 = new Manager("Rick", "Smith", 45000);
Employee *s2 = new Manager("Dick", "Smith", 25000);
Insurance *a = new Auto("John", "Smith", "ford", "mustang", 123456789, 600, 796);
Insurance *b = new Home("Larry", "Smith", 45, 670, 3220, 4120);

    
e->addSalesperson(s1);
e->addSalesperson(s2);
s1->addSale(a);
s2->addSale(b);

employee_list.push_back(e);
employee_list.push_back(s1);
employee_list.push_back(s2);
sales_list.push_back(a);
sales_list.push_back(b);

e->print_staff_sales(cout);


Employee class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Employee{
protected:
    string first_name;
    string last_name;
    string employee_type;
    float base_salary;
    float commission;
    float total_salary;
    vector<Employee *> staff_list;
public:
    vector&lt;Insurance *&gt; sale_list; // holders pointers to the abstract Insurance class objects
    Employee():first_name(""), last_name(""), base_salary(0){ };
    Employee(string fn, string ln, float bs):first_name(fn), last_name(ln), base_salary(bs){ };

    void addSale( Insurance * );
    void addSalesperson( Employee * );

    //Other methods left out
};


Manager Class:
1
2
3
4
5
6
7
8
9
10
class Manager: public Employee{
private:
    float staff_commission;
public:
    // Constructors
    Manager(): Employee(){ employee_type = "Manager"; };
    Manager(string fn, string ln, float bs): Employee(fn, ln, bs){ employee_type = "Manager";

    //Other methods left out
};


Print method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ostream &Manager::print_staff_sales( ostream &strm ){
    strm << "Manager: " << first_name << " " << last_name << endl;
    
    Salesperson *s_temp;
    for (int i = 0; i < staff_list.size(); ++i){
        s_temp = staff_list.at(i);
        
        Insurance *i_temp;
        for (int i = 0; i < s_temp->sale_list.size(); ++i){
            i_temp = sale_list.at(i);
            strm<< *i_temp << endl;
        }
    }
    
    strm<< endl;
    return strm;
}
Last edited on
First of all, why would you be having a vector of pointers, and not actual types(ie: vector<Employer>)?

Aceix.
Employee is an abstract class (there are pure virtual functions in there that I left out), you cant have a vector of abstract class objects. I guess that would have been worth noting haha
Your design is really confusing. Why do you have a staff_list in Employee class? This means each Employee and its derived class has its own staff_list. I think you do this because each Manager have a list of Salesperson managed by it. I suggest adding a vector<Salesperson*> in the Manager class instead.

Again, It seems that every employee have a sale_list. But I guess Manager does not sells. So in the print method i_temp = sale_list.at(i); is accessing the Manager's empty sale_list, which I think will throw an out_of_range exception (but you said it prints nothing). You should write i_temp = s_temp->sale_list.at(i); in order to access the Salesperson's sale_list. More importantly, you better remove this sale_list in the Employee class but add it in the Salesperson class.
Topic archived. No new replies allowed.