1 Replies - 210 Views - Last Post: 31 July 2012 - 01:15 AM Rate Topic: -----

#1 Phox  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 17-December 10

Base class pointer to vector

Posted 30 July 2012 - 09:07 PM

Ok, so in case 2 of main I'm trying to add instances of Salespersons to vector <Salesperson*> manList defined in Manager.h. Basically I need a list of salespersons to be stored for each manager. (The manager's commission is dependent on the sales of the salespersons under him.)

I'm trying to accomplish this with
emp2->popList(empList[numSales]);


I have a feeling i'm mixing up my types.. is it a pointer, not a pointer, etc.

Errors:

C:\Users\supercomputer\Desktop\Data Structures\Project4\main.cpp||In function 'int main()':|
C:\Users\supercomputer\Desktop\Data Structures\Project4\main.cpp|94|warning: comparison between signed and unsigned integer expressions|
obj\Debug\main.o||In function `main':|
C:\Users\supercomputer\Desktop\Data Structures\Project4\main.cpp|76|undefined reference to `Employee::popList(Employee*)'|
||=== Build finished: 1 errors, 1 warnings ===|


Appreciate the help. Thanks for your time

main.cpp:

#include <cstdlib>
#include <iostream>
#include <vector>
#include "Auto.h"
#include "Home.h"
#include "Life.h"
#include "Salesperson.h"
#include "Manager.h"


using namespace std;

int main()
{

vector<Employee *> empList;

do{
    cout << endl << "Insurance Record Keeping System" << endl;
    cout << "---------------" << endl;
    cout << "1. Add salesperson" << endl;
    cout << "2. Add manager" << endl;
    cout << "3. Print employee list" << endl;
    cout << "4. Add policy" << endl;

    int menuChoice;

    cout << "Enter menu choice: ";
    cin >> menuChoice;

    switch (menuChoice)
    {
        case 1:
        {
            string tempNameS;
            float tempBaseSalaryS;

            cout << endl << "Creating Salesperson...";
            cout << endl << "Enter name (ex: John_Doe): ";
            cin >> tempNameS;
            cout << "Enter base salary: ";
            cin >> tempBaseSalaryS;

            Employee * emp1 = new Salesperson(tempNameS, tempBaseSalaryS);
            //shape1->calcArea();

            empList.push_back(emp1);


            break;
        }

        case 2:
        {
            string tempNameM;
            float tempBaseSalaryM;

            cout << endl << "Creating Manager...";
            cout << endl << "Enter name (ex: John_Doe): ";
            cin >> tempNameM;
            cout << "Enter base salary: ";
            cin >> tempBaseSalaryM;

            Employee * emp2 = new Manager(tempNameM, tempBaseSalaryM);

            cout << "Enter number of employees managed: ";
            int numEmp;
            cin >> numEmp;

            for (int i = 0; i < numEmp; i++)
            {
                cout << "Enter index number of salesperson to add: ";
                int numSales = 0;
                cin >> numSales;

                emp2->popList(tempSales);
 
                //emp2.manList.push_back(empList[numSales]);
            }
            //shape2->calcArea();

            empList.push_back(emp2);



            break;
        }

        case 3:
        {
            cout << endl << "Printing all employees:" << endl;


            for (int v =0; v < empList.size(); v++)
            {
                cout << "[" <<  v << "] " << empList[v]->getName() << endl;
            }

            break;
        }

        case 4:
        {
      //      cout << endl << "Printing all areas:" << endl;


      //      for (int i =0; i < shapes.size(); i++)
       //     {
       //         shapes[i]->printArea();
        //    }

            break;
        }

        case 5:
        {
            exit(1);
        }
    };
}
while(1);


    return 0;
}



Employee.h:


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
using namespace std;

class Employee
{
      protected:
              string name;
              float baseSalary;
              float commTotal;
              float totalSalary;
              float finalComm;
              vector <Policy*> polVect;

      public:
             virtual void calcFinalComm() = 0;
             virtual void calcTotalSal() = 0;
             void popList(Employee*);
             string getName()
             {
                 return name;
             }
             //virtual void printArea() = 0;
            // float getArea(){return area;}

};

#endif



Salesperson.h:

#include "Employee.h"

class Salesperson: public Employee
{

    public:

        Salesperson(string a="", float b=0)
        {
            name = a;
            baseSalary = b;
        }

        void calcFinalComm()
        {
            finalComm = (commTotal) + (commTotal * .05);
        }

        void calcTotalSal()
        {
            totalSalary = baseSalary + finalComm;
        }

        void popList(Salesperson *)
        {

        }
};


Manager.h:

#include "Employee.h"

class Manager: public Employee
{
    protected:
        vector <Salesperson*> manList;
        float empTotal;

    public:



        Manager(string a="", float b=0)
        {
            name = a;
            baseSalary = b;
        }

       void popList(Salesperson* s)
        {
        manList.push_back(s);
        }

        void calcFinalComm()
        {
            finalComm = (commTotal) + (commTotal * .3) + (empTotal * .05);
        }

         void calcTotalSal()
        {
            totalSalary = baseSalary + finalComm;
        }
};

This post has been edited by Phox: 30 July 2012 - 09:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Base class pointer to vector

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Base class pointer to vector

Posted 31 July 2012 - 01:15 AM

You're calling Employee::popList(Employee*), which is not defined anywhere. You've declared it in Employee.h, but you never define it.

You are defining popList(SalesPerson*) in the SalesPerson and Manager classes, but that doesn't change anything, because

1. popList is not virtual which means that the compiler will decide at compile-time which function to call independently of the actual class of the pointed-to object and
2. popList(SalesPerson*) is not a compatible signature for popList(Employee*), so even if Employee::popList(Employee*) were virtual, popList(SalesPerson*) would not override it.

Either way you still need to define Employee::popList unless you make it purely virtual.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1