0

Hey can anyone help me with the below errors please?

vector.cpp: In function 'void printEmployees(const std::vector<employee, std::allocator<employee> >&)':
vector.cpp:104: error: passing 'const employee' as 'this' argument of 'void employee::printEmployee()' discards qualifiers

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>

#include <stdlib.h>

using namespace std;

class employee
{
   /* Employee class to contain employee data
   */
   
   private:
      string surname;
      double hourlyRate;
     int empNumber;
     
   public:
      employee() {
         hourlyRate = -1;
         empNumber = -1;
         surname = "";
      }
      
      employee(const employee &other) :
         surname(other.surname),
         hourlyRate(other.hourlyRate),
         empNumber(other.empNumber)
      {
         // copy constructor
      }
      
      void setEmployee(const string &name, double rate, int num) { 
         surname = name;
         hourlyRate = rate;
         empNumber = num;
      }
      
      const string& getSurname() const {
         return surname;
      }
      
      void printEmployee() {
         cout << fixed << setprecision(2);
         cout << setw(20) << left << surname << setw(4) << empNumber << "  " << hourlyRate << "\n";
      }
      
      void loadEmployee(ifstream &fin) {
         fin >> surname;
         fin >> hourlyRate;
         fin >> empNumber;
      }
      
};
      
void swap(vector<employee> employees, int a, int b);
void sortEmployees(vector<employee> &employees);
void printEmployees(const vector<employee> &employees);
void loadEmployees(vector<employee> &employees, const char *file);

int main(int argc, char *argv[])
{
   vector<employee> employees;

   if (argc != 2) {
      cout << "Syntax : employee employeefile\n";
      return 0;
   }

   loadEmployees(employees, argv[1]);
   printEmployees(employees);
   sortEmployees(employees);
   printEmployees(employees);

   return 0;
}

void loadEmployees(vector<employee> &employees, const char *file)
{
   ifstream fin;
   employee emp;
   
   fin.open(file);
   if (!fin) {
      cout << "Unable to read from " << file << "\n";
      exit(0);
   }
   
   while (!fin.eof()) {
      emp.loadEmployee(fin);
      employees.push_back(emp);
   }
   fin.close();   
}

void printEmployees(const vector<employee> &employees)
{
   unsigned int i;

   for (i=0; i<employees.size(); i++) {
      employees[i].printEmployee();
   }
   printf("\n");
}

void swap(vector<employee> employees, int a, int b)
{
   employee temp(employees[a]);

   employees[a] = employees[b];
   employees[b] = temp;
}

void sortEmployees(vector<employee> &employees)
{
   /* use selection sort to order employees, 
      in employee name order
   */

   int number = employees.size();
   int inner, outer, max;

   for (outer=number-1; outer>0; outer--)
   {
      // run though array number of times
      max = 0;
      for (inner=1; inner<=outer; inner++)
      {
         // find alphabeticaly largest surname
         if (employees[inner].getSurname() > employees[max].getSurname())
            max = inner;
      }
      if (max != outer)
      {
         // swap largest with last element in array
         swap(employees, max, outer);
      }
   }
}
2
Contributors
1
Reply
4
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

remove the const keyword in the parameter to that function on line 99. const does not allow the function to call non-const methods of the class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.