C++ "No suitable constructor exists to convert from <default constructor> to parameterized constructor

StackOverflow https://stackoverflow.com/questions/18940510

Domanda

I'm sorry for asking this, as it's probably answered somewhere on here, but my searches so far have been fruitless.

If I use my parameterized constructor, I can pass my class object to my output function and all is well. If I use the default constructor, it fails with:

1>c:\<path>\project_04.cpp(152): error C2664: 'printCheck' : cannot convert parameter 1 from 'AdamsEmployee (__cdecl *)(void)' to 'AdamsEmployee'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

The mouse-over for my object when I try to pass it to the output function says:

Error: No suitable constructor exists to convert from "AdamsEmployee ()" to "AdamsEmployee"

Here is my default constructor:

AdamsEmployee::AdamsEmployee()
{
    AdamsEmployee::employeeNumber = -1;
    AdamsEmployee::employeeName = "";
    AdamsEmployee::employeeAddress = "";
    AdamsEmployee::employeePhone = "";
    AdamsEmployee::employeeHourlyWage = 0.0;
    AdamsEmployee::employeeHoursWorked = 0.0;
}

Here is my parameterized constructor:

AdamsEmployee::AdamsEmployee(int employeeNumber, string employeeName, string
employeeAddress, string employeePhone, double employeeHourlyWage,
doubleemployeeHoursWorked )
{
    AdamsEmployee::employeeNumber = employeeNumber;
    AdamsEmployee::employeeName = employeeName;
    AdamsEmployee::employeeAddress = employeeAddress;
    AdamsEmployee::employeePhone = employeePhone;
    AdamsEmployee::employeeHourlyWage = employeeHourlyWage;
    AdamsEmployee::employeeHoursWorked = employeeHoursWorked;
}

The line that calls the output:

printCheck( emp1 );

The output function:

void printCheck( AdamsEmployee employee )
{
// Display the mock paycheck.
cout << "----------------------------------H&H Systems----------------------------------" << endl;
cout << "\nPay to the order of " << employee.getName() << ".....$" << employee.calcPay() << endl;
// Display the simulated paystub.
cout << "\nGoliath National Bank" << endl;
cout << "-------------------------------------------------------------------------------" << endl;
cout << "Hours worked: " << employee.getHoursWorked() << endl;
cout << "Hourly wage: " << employee.getWage() << endl;
} // End printCheck()

If I add parameters, everything works. Searches return a lot of situations that do not seem to apply. Do you need any more information?

What am I doing wrong?

Edit: Thanks for all the help!

È stato utile?

Soluzione

Your error says you're passing a function, as if you're declared AdamsEmployee emp1(). This is probably due to parsing ambiguity, as one comment mentions. It's so common there's an entire stackoverflow tag for it: https://stackoverflow.com/questions/tagged/most-vexing-parse

Altri suggerimenti

You are probably missing a declaration for the constructor in the class for the default constructor.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top