首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:候选构造函数不可行:需要5个参数,但提供了0

错误:候选构造函数不可行:需要5个参数,但提供了0
EN

Stack Overflow用户
提问于 2021-09-29 01:04:01
回答 1查看 580关注 0票数 0

我的项目有问题。它一直说构造函数有问题,但我找不到问题。对于代码的长度,我很抱歉,我肯定我错过了一些愚蠢的东西。

原谅我的标题..。DateTime.h

代码语言:javascript
复制
    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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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();
}

然后我有了一个名为.csv的EmployeeList.csv文件

这是我收到的错误信息

代码语言:javascript
复制
 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
EN

回答 1

Stack Overflow用户

发布于 2021-09-29 01:08:00

employeeList.resize(newSize)std::vector中的操作需要包含元素的默认构造函数,这在类Employee中是遗漏的。这对于在容器中创建空元素是必要的。

如果由于某种原因无法容纳/负担默认构造函数,只需添加默认构造函数,或者存储指针。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69369956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档