Hello, for laboratory I need to make an array of 5 static and 5 dynamic objects of a class, but when creating, an error pops up: Class has more than one default constructor. Since I have 3 constructors:

Employee(string Name = " ", int Age = 1, int Salary = 1, int Experience = 1); Employee(); Employee(const Employee &Sclass); 

Here is the function to create arrays:

  void DemonstrationM() { Employee *membersD, membersP[5]; try { membersD = new Employee[5]; } catch (bad_alloc xa) { cout << "Не удалось добавить в дим. масив " << endl; system("Pause"); } for (int i = 0; i < Members.size(); ++i) { if (i < 5) membersD[i] = Members[i]; else { membersP[i - 5] = Members[i]; } } cout << membersP[2].getAge(1) << endl; cout << membersD[3].getAge(1) << endl; main(); } 

    2 answers 2

    You have these two constructors

     Employee(string Name = " ", int Age = 1, int Salary = 1, int Experience = 1); Employee(); 

    are the default constructors, since each of them can be called without arguments. And when declaring arrays, you use just the default constructor. Therefore, there is an ambiguity. The compiler does not know which of these two constructors to call when creating array elements.

    Remove the default argument from the first parameter. for example

     Employee( const std::string &Name, int Age = 1, int Salary = 1, int Experience = 1); 

    You can also declare it with the explicit function specifier to avoid implicit conversions. For example,

     explicit Employee( const std::string &Name, int Age = 1, int Salary = 1, int Experience = 1); 

    Keep in mind that you cannot recursively call the main function in a C ++ program. This can only be done in C. Therefore, this code is incorrect.

      //... cout << membersP[2].getAge(1) << endl; cout << membersD[3].getAge(1) << endl; main(); ^^^^^^^ } 

      Without touching everything else, just why swearing:

       Employee(string Name = " ", int Age = 1, int Salary = 1, int Experience = 1); Employee(); 

      Here, for example

       membersD = new Employee[5]; 

      the compiler just doesn't know which one to choose! All the first is equivalent to the second - when calling without arguments!

      And still - do not transfer in catch exception on value, it is better to do it under the link.