简体   繁体   English

C ++“没有合适的构造函数可供转换 <default constructor> 参数化构造函数

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

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! 编辑:感谢您的帮助!

Your error says you're passing a function, as if you're declared AdamsEmployee emp1() . 您的错误表示您正在传递函数,就像您已声明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 它是如此常见,它有一个完整的stackoverflow标记: https//stackoverflow.com/questions/tagged/most-vexing-parse

您可能缺少默认构造函数的类中构造函数的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 不存在合适的构造函数来转换为 - C++ no suitable constructor exists to convert from to C++ 小问题:不存在合适的构造函数来从“void”转换 - C++ minor problem: no suitable constructor exists to convert from “void” 没有合适的构造函数可以从“test *”转换为“test”,构造函数, - no suitable constructor exists to convert from “test *” to “test”, constructor, c++ 不存在合适的构造函数来将“int”转换为“std::pair”<int, int> ”</int,> - c++ no suitable constructor exists to convert from “int” to “std::pair<int, int>” C++ 父 Class 调用默认构造函数而不是参数化构造函数 - C++ Parent Class Default Constructor Called Instead of Parameterized Constructor 当具有默认参数的构造函数存在时,c++ 构造函数中的歧义 - Ambiguity in c++ constructor when a constructor with default argument exists 子代不存在默认构造函数-C ++ - No default constructor exists for child - c++ C++:父 class 不存在默认构造函数 - C++: No default constructor exists for parent class 没有合适的构造函数可将“ const char [7]”转换为“ TopicA” - No suitable constructor exists to convert from “const char[7]” to “TopicA” 不存在将“哑指针”转换为“智能指针”的合适构造函数 - No suitable constructor exists to convert from “dumb pointer” to “smart pointer”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM