0

I'm having an issue with my project. It keeps saying there is an issue with a constructor, but I can't find the issue. I'm sorry for the length of the code, and I'm sure it is something silly I am missing.

Pardon my headers... DateTime.h

    using namespace std;

//Constant array containing the days in the given month
const int DAYS_OF_MONTH[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

//Custom Date/Time format with a year that 1/1 is a sunday (Ex. 2017) to make things simpler
class DateTime {
  int month, day, hour, minute;

  public:
  //Creates a basic blank DateTime
  DateTime() {
    month = 1;
    day = 1;
    hour = 0;
    minute = 0;
  }

  //Creates a DateTime with a specific Date and Time
  DateTime(int m, int d, int h, int min) {
    month = m;
    day = d;
    hour = h;
    minute = min;
  }

  //Adds the days to the date and updates the month if the day goes over
  void addDays(int d) {
    try {
    for(int i = 0; i < d; i++) {
      day++;
      if(day > DAYS_OF_MONTH[month-1]) {
        month++;
        day = 1;
      }
    }
    }
    catch(string ex) {

    }
  }

  //Adds a week onto the Date
  void addWeeks(int w) {
    for(int i = 0; i < w; i++) {
      addDays(7);
    }
  }

  //Accessors
  int getMonth() {
    return month;
  }
  int getDay() {
    return day;
  }
  int getHour() {
    return hour;
  }
  int getMinute() {
    return minute;
  }

  //Mutators
  void setMonth(int newM) {
    month = newM;
  }
  void setDay(int newD) {
    day = newD;
  }
  void setHour(int newH) {
    hour = newH;
  }
  void setMinute(int newM) {
    minute = newM;
  }
};`

Day.h

#include<string>
#include "DateTime.h"

class Day {
  DateTime startTime;
  DateTime endTime;
  int weekOfYear;
  int dayOfWeek;

  public:
  //Creates a blank day
  Day() {
    startTime = DateTime(1, 1, 0, 0);
    endTime = DateTime(1, 1, 0, 0);
    weekOfYear = 0;
    dayOfWeek = 0;
  }

  //Creates a blank day with a specific date
  Day(int wOfY, int dOfW) {
    startTime = DateTime(1, 1, 0, 0);
    endTime = DateTime(1, 1, 0, 0);
    weekOfYear = wOfY;
    dayOfWeek = dOfW;

    //Add weeks of the year and days of the week to the date
    startTime.addDays(dOfW-1);
    startTime.addWeeks(wOfY-1);
    endTime.addDays(dOfW-1);
    endTime.addWeeks(wOfY-1);
  }

  //Create a filled day with specific date and start/end times
  Day(int wOfY, int dOfW, int startHr, int startMin, int endHr, int endMin) {
    startTime = DateTime(1, 1, startHr, startMin);
    endTime = DateTime(1, 1, endHr, endMin);
    weekOfYear = wOfY;
    dayOfWeek = dOfW;
  }

  //Accessors
  DateTime getStartTime() {
    return startTime;
  }
  DateTime getEndTime() {
    return endTime;
  }
  int getWeekOfYear() {
    return weekOfYear;
  }
  int getDayOfWeek() {
    return dayOfWeek;
  }

  //Mutators
  void setStartTime(DateTime newTime) {
    startTime = newTime;
  }
  void setEndTime(DateTime newTime) {
    endTime = newTime;
  }
  void setWeekOfYear(int newWOfY) {
    weekOfYear = newWOfY;
  }
  void setDayOfWeek(int newDOfW) {
    dayOfWeek = newDOfW;
  }

  //Calculates the amount of time between startTime and endTime
  float getDailyHours() {
    if((float)endTime.getHour() - (float)startTime.getHour() >=1) {
      return ((60- (float)startTime.getMinute())/60) + ((float)endTime.getHour() - (float)startTime.getHour() - 1) + ((float)endTime.getMinute() / 60);
    }
    else {
      return ((float)endTime.getMinute() - (float)startTime.getMinute()) / 60;
    }
  }

  //Returns the data of the day in the format to save to the file
  string toString() {
   return string(",") + to_string(dayOfWeek) + string(",") + to_string(startTime.getHour()) + string(",") + to_string(startTime.getMinute()) + string(",") + to_string(endTime.getHour()) + string(",") + to_string(endTime.getMinute());
  }
};

Week.h

#include "Day.h"
#include <vector>
using namespace std;

class Week {
  vector<Day> dayList;
  float totalWeeklyHours;
  int weekOfYear;

  public:
  //Creates a basic week
  Week(int wOfY) {
    //dayList.resize(7);
    weekOfYear = wOfY;
    
    for(int i = 0; i < 7; i++) {
      dayList.push_back(Day(weekOfYear, i));
    }
  }

  //Creates a week given a dayList
  Week(int wOfY, vector<Day> dL) {
    //dayList.resize(7);
    weekOfYear = wOfY;
    for(int i = 0; i < 7; i++) {
      dayList.push_back(dL.at(i));
    }
  }

  //Accessors
  int getWeekOfYear() {
    return weekOfYear;
  }
  vector<Day> getDayList() {
    return dayList;
  }

  //Adds a day to dayList from EmployeeManager.readFile()
  void addDay(int wOfY, int dOfW, int startHr, int startMin, int endHr, int endMin) {
    Day temp = Day(wOfY, dOfW, startHr, startMin, endHr, endMin);
    dayList.at(dOfW) = temp;
    weekOfYear = temp.getWeekOfYear();
  }

  //Adds a day from Employee.addWeek()
  void addDay(Day newDay, int dOfW) {
    dayList.at(dOfW) = newDay;
    weekOfYear = newDay.getWeekOfYear();
  }

  //Calculates the total amount of hours scheduled in the week
  float getTotalWeeklyHours() {
    totalWeeklyHours = 0;
    for(int i = 0; i < dayList.size(); i++) {
      totalWeeklyHours += dayList.at(i).getDailyHours();
    }

    return totalWeeklyHours;
  }

  //Returns the data of the week in the format to save in the file
  string toString() {
    return (string)"," + to_string(weekOfYear) + dayList.at(0).toString() + dayList.at(1).toString() + dayList.at(2).toString() + dayList.at(3).toString() + dayList.at(4).toString() + dayList.at(5).toString() + dayList.at(6).toString();
  }
};

Person.h

#include <string>
using namespace std;

class Person {
  public:
    string firstName;
    string lastName;
    int employeeID;
    int employeePIN;
    float hourlyRateOfPay;

  public:
    Person() {
      firstName = "John";
      lastName = "Doe";
      employeeID = 0;
      employeePIN = 0;
      hourlyRateOfPay = 0;
    }

    //Accessors
    string getFirstName() {
      return firstName;
    }
    string getLastName() {
      return lastName;
    }
    int getEmployeeID() {
      return employeeID;
    }
    int getEmployeePIN() {
      return employeePIN;
    }
    float getHourlyRateOfPay() {
      return hourlyRateOfPay;
    }

    //Mutators
    void setFirstname(string newName) {
      firstName = newName;
    }
    void setLastName(string newName) {
      lastName = newName;
    }
    void setEmployeeID(int newID) {
      employeeID = newID;
    }
    void setEmployeePIN(int newPIN) {
      employeePIN = newPIN;
    }
    void setHourlyRateOfPay(float newPay) {
      hourlyRateOfPay = newPay;
    }

    //Abstract methods
    string viewSchedule(int wOfY);
    float viewPay(int wOfY);
};

Employee.h

#include "Person.h"
#include "Week.h"
using namespace std;

class Employee : public Person {
  vector<Week> weekList;

  public:
    Employee(int empID, int empPIN, string fName, string lName, float hROP) {
      employeeID = empID;
      employeePIN = empPIN;
      firstName = fName;
      lastName = lName;
      hourlyRateOfPay = hROP;
      
      /*
      setEmployeeID(empID);
      setEmployeePIN(empPIN);
      setFirstName(fname);
      setLastName(lname);
      setHourlyRateOfPay(hROP);
      */

      for(int i = 0; i < 52; i++) {
        weekList.push_back(Week(i));
      }
    }

    Week getWeek(int wOfY) {
      return weekList.at(wOfY);
    }

    //Adds a week to the weekList from EmployeeManager.readFile()
    void addWeek(int wOfY, vector<Day> dL) {
      for(int i = 0; i < 7; i++) {
        weekList.at(wOfY).addDay(dL.at(i), i);
      }
    }

    //Adds a week from client
    void addWeek(int wOfY, Week newWeek) {
      weekList.at(wOfY) = newWeek;
    }

    //Returns a specified wek to be viewed by the user
    string viewSchedule(int wOfY) {
      return weekList.at(wOfY).toString();
    }

    //Returns the amount of pay for a specified week to be viewed by the user
    float viewPay(int wOfY) {
      return hourlyRateOfPay * (weekList.at(wOfY).getTotalWeeklyHours());
    }

    //Returns the data of the employee in the format to save to the readFile
    string toString() {
      return firstName + "," + lastName + "," + to_string(employeeID) + "," + to_string(employeePIN) + "," + to_string(hourlyRateOfPay);
    }
};

EmployeeManager.h

#include "Employee.h"
#include <fstream>
#include <sstream>
using namespace std;

class EmployeeManager {
  vector<Employee> employeeList;

  public:
  EmployeeManager() {
    //readFile();
  }

  void readFile() {
  //Variables
  int empID, empPIN, wOfY, dOfW, startHr, startMin, endHr, endMin;
  float hROP;
  string fname, lname;

  ifstream input;
  input.open("EmployeeList.csv");
  while(!input.eof()) {
    input >> fname >> lname >> empID >> empPIN >> hROP;

    //Create temp Employee
    Employee tempEmployee = Employee(empID, empPIN, fname, lname, hROP);

    for(int i = 0; i < 52; i++) {
      Week tempWeek = Week(i);
      input >> wOfY;
      for(int j = 0; j < 7; j++) {
        input >> dOfW >> startHr >> startMin >> endHr >> endMin;
        tempWeek.addDay(wOfY, dOfW, startHr, startMin, endHr, endMin);
      }
      tempEmployee.addWeek(wOfY, tempWeek.getDayList());
    }
    addEmployee(tempEmployee);
  }
  input.close();
  }

  void saveFile() {
    ofstream output("EmployeeList.csv");

    for(int i = 0; i < employeeList.size(); i++) {
      output << employeeList.at(i).toString();
      for(int j = 0; j < 52; j++) {
        output << employeeList.at(i).viewSchedule(j);
      }
      output << "\n";
    }
    output.close();
  }

  //Adds a new employee to the employeeList from readFile
  void addEmployee(Employee newEmp) {
    //employeeList.resize(employeeList.size()*2);
    try{
      employeeList.push_back(newEmp);
    }
    catch(out_of_range ex) {
      cout << "EmployeeList out_of_range" << endl;
      int newSize = employeeList.size() * 2;
      employeeList.resize(newSize);
      addEmployee(newEmp);
    }
    
  }

  //Adds a new employee to the employeeList from client
  void addEmployee(int empID, int empPIN, string fname, string lname, float hROP) {
    Employee newEmp = Employee(empID, empPIN, fname, lname, hROP);
    //employeeList.resize(employeeList.size()*2);
    try {
      employeeList.push_back(newEmp);
    }
    catch(out_of_range ex) {
      cout << "EmployeeList out_of_range" << endl;
      int newSize = employeeList.size() * 2;
      employeeList.resize(newSize);
      addEmployee(newEmp);
    }
  }

  bool validateLogin(int empID, int empPIN) {
    for(int i = 0; i < employeeList.size(); i++) {
      if(employeeList.at(i).getEmployeeID() == empID) {
        if(employeeList.at(i).getEmployeePIN() == empPIN) {
          return true;
        }
      }
    }
    return false;
  }

  bool isIDAvailable(int empID) {
    for(int i = 0; i < employeeList.size(); i++) {
      if(employeeList.at(i).getEmployeeID() == empID) {
        return false;
      }
    }
    return true;
  }
};

main.cpp

#include <iostream>
#include "EmployeeManager.h"

int main() {
  Day d1 = Day(9,6);
  cout << d1.toString() << endl;

  Week w1 = Week(1);
  cout <<"\n\n" << w1.toString() << endl;
  
  Employee e1 = Employee(12345, 9876, "Test", "Employee", 15.60);
  cout << "\n\n" << e1.toString();
  for(int i = 0; i < 52; i++) {
    cout << e1.viewSchedule(i);
  }
  cout << endl;

  EmployeeManager em1 = EmployeeManager();
  em1.saveFile();
}

Then I had a .csv file named EmployeeList.csv

This is the Error Message I was given

 clang++-7 -pthread -std=c++17 -o main main.cpp
In file included from main.cpp:2:
In file included from ./EmployeeManager.h:1:
In file included from ./Employee.h:2:
In file included from ./Week.h:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/vector:62:
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_construct.h:75:38: error: 
      no matching constructor for initialization of 'Employee'
    { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
                                     ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_uninitialized.h:527:8: note: 
      in instantiation of function template specialization
      'std::_Construct<Employee>' requested here
                std::_Construct(std::__addressof(*__cur));
                     ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_uninitialized.h:583:2: note: 
      in instantiation of function template specialization
      'std::__uninitialized_default_n_1<false>::__uninit_default_n<Employee *,
      unsigned long>' requested here
        __uninit_default_n(__first, __n);
        ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_uninitialized.h:645:19: note: 
      in instantiation of function template specialization
      'std::__uninitialized_default_n<Employee *, unsigned long>' requested here
    { return std::__uninitialized_default_n(__first, __n); }
                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/vector.tcc:596:8: note: 
      in instantiation of function template specialization
      'std::__uninitialized_default_n_a<Employee *, unsigned long, Employee>'
      requested here
                std::__uninitialized_default_n_a(this->_M_impl._M_finish,
                     ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:827:4: note: 
      in instantiation of member function 'std::vector<Employee,
      std::allocator<Employee> >::_M_default_append' requested here
          _M_default_append(__new_size - size());
          ^
./EmployeeManager.h:64:20: note: in instantiation of member function
      'std::vector<Employee, std::allocator<Employee> >::resize' requested here
      employeeList.resize(newSize);
                   ^
./Employee.h:5:7: note: candidate constructor (the implicit copy constructor)
      not viable: requires 1 argument, but 0 were provided
class Employee : public Person {
      ^
./Employee.h:5:7: note: candidate constructor (the implicit move constructor)
      not viable: requires 1 argument, but 0 were provided
./Employee.h:9:5: note: candidate constructor not viable: requires 5 arguments,
      but 0 were provided
    Employee(int empID, int empPIN, string fName, string lName, float hROP) {
    ^
1 error generated.
exit status 1
SeanR
  • 11
  • 1
  • 2
    It'd be nice to have a [mcve]. Emphasis on "minimal". – Nate Eldredge Sep 29 '21 at 01:06
  • Please see https://stackoverflow.com/help/minimal-reproducible-example for advice on how to reduce the size of the code – M.M Sep 29 '21 at 01:07
  • Totally unrelated: `while(!input.eof())` [is not your friend.](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Sep 29 '21 at 01:08
  • As a minor nitpick: Your headers Employee.h and EmployeeManager.h use vectors. You are doubtless replying on having included `` in Week.h, but any files that are using vectors should `#include `. _This is not exclusive to STL vectors._ – Chris Sep 29 '21 at 01:12
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Oct 07 '21 at 16:09

1 Answers1

0

Operations like employeeList.resize(newSize) in std::vector require a default constructor for the containing element, which is missed in the class Employee. This is necessary to create the empty elements in the container.

Just add the default constructor, or store pointers instead if you can't accommodate/afford the default constructor for some reason.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Damir Tenishev
  • 1,275
  • 3
  • 14
  • Before taking this advice, make sure that a default constructor for your class makes sense. If it doesn't, `reserve` space and `emplace_back` as necessary. – user4581301 Sep 29 '21 at 01:10