C++ "没有合适的构造函数可以从<默认构造函数>转换为参数化构造函数
C++ "No suitable constructor exists to convert from <default constructor> to parameterized constructor
我很抱歉问这个问题,因为它可能在这里的某个地方得到了回答,但到目前为止我的搜索都没有结果。
如果我使用参数化构造函数,我可以将我的类对象传递给我的输出函数,一切都很好。 如果我使用默认构造函数,它会失败并显示:
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
当我尝试将对象传递给输出函数时,我的对象的鼠标悬停显示:
Error: No suitable constructor exists to convert from "AdamsEmployee ()" to "AdamsEmployee"
这是我的默认构造函数:
AdamsEmployee::AdamsEmployee()
{
AdamsEmployee::employeeNumber = -1;
AdamsEmployee::employeeName = "";
AdamsEmployee::employeeAddress = "";
AdamsEmployee::employeePhone = "";
AdamsEmployee::employeeHourlyWage = 0.0;
AdamsEmployee::employeeHoursWorked = 0.0;
}
这是我的参数化构造函数:
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;
}
调用输出的行:
printCheck( emp1 );
输出功能:
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()
如果我添加参数,一切正常。 搜索返回许多似乎不适用的情况。 您需要更多信息吗?
我做错了什么?
编辑:感谢所有的帮助!
您的错误表明您正在传递一个函数,就好像您被声明为 AdamsEmployee emp1()
一样。 正如一条评论所提到的,这可能是由于解析歧义。 它是如此普遍,以至于它有一个完整的堆栈溢出标签:https://stackoverflow.com/questions/tagged/most-vexing-parse
您可能缺少默认构造函数类中构造函数的声明。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 当从函数参数中的临时值调用复制构造函数时
- 在c++构造函数中使用随机字符串生成器
- 一对向量构造函数:初始值设定项列表与显式构造
- 从构造函数抛出异常时如何克服内存泄漏
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 具有默认模板类型的默认构造函数的类型推导
- 使用dynamic_cast和构造函数时出错
- 在c++中使用向量时,如何调用构造函数和析构函数
- std::pair的默认构造函数<>将基本类型(int等)设置为零