0

I am learning c++ and have a construct error that I can't figure out. says no default constructor where it says Employees* Employee = new Employees[y]; It is very likely that it is a simple mistake. Code:

#include <string>
using namespace std;
class Employees {
private:
    int age;
    int Year_pay;
    string Name;
public:
    Employees(string N, int a, int p) {
        setName(N);
        setAge(a);
        setpay(p);
    }
    int getAge() {
        return age;
    }
    void setAge(int ag) {
        age = ag;
    }
    string getName() {
        return Name;
    }
    void setName(string Na) {
        Name = Na;
    }
    int getPay() {
        return Year_pay;
    }
    void setpay(int Pa) {
        Year_pay = Pa;
    }
};
int main()
{
    int y = 5;
    Employees* Employee = new Employees[y];
    for (int i = 0; i < y; ++i) {
        string Tname = "";
       cout << "What is Employee Name?";
        cin >> Tname;
        int Tage = 0;
        cout << "What is Employee age?";
        cin >> Tage;
        int Tpay = 0;
        cout << "What is Employee yearly pay?";
        cin >> Tage;
        Employee[i](Tname, Tage, Tpay);
    }
    cout << Employee[6].getAge()
    return 0;
}
  • You only have one constructor, and it *requires* arguments, You cannot use it as part of a native array-allocated syntax (be it dynamic or automatic; makes no difference). – WhozCraig Jan 12 '21 at 01:18
  • @WhozCraig What do I need to do, This is my first time using classes in C++ – Colin Foster Jan 12 '21 at 01:50
  • Read the linked duplicate carefully. It describes the problem and several potential work-arounds. – WhozCraig Jan 12 '21 at 01:53
  • This is something that should be explained in every C++ textbook. You can also reread the last chapter in your textbook, that you read before attempting this practice problem, and it should have a complete explanation of how to make classes work like that. – Sam Varshavchik Jan 12 '21 at 02:45

0 Answers0