错误:与呼叫"(家庭)"不匹配
Error: no match for call to '(Household)
因此,我基本上有一个错误,对我来说是没有意义的。我已经尝试了所有事情,但似乎没有任何作用,所以我认为你们将能够帮助我。顺便说一句,这是我在本网站上的第一篇文章。
我正在研究一个涉及一个名为" house.cc,h"和测试程序的课程。
这是家庭。H(有关的代码)
class Household{
public:
// Default constructor
Household(std::string nme, std::string adrs, int peeps, int ncome);
Household();
Household(std::string& nme, std::string& adrs, int& peeps, int& ncome);
这是我的家庭.cc文件
// constructors
Household::Household(std::string nme, std::string adrs, int peeps, int ncome){
name = nme;
address = adrs;
numpeople = peeps;
income = ncome;
}
Household::Household(std::string& nme, std::string& adrs, int& peeps, int& ncome){
name =nme;
address = adrs;
numpeople=peeps;
income=ncome;
}
Household::Household(){
name = "";
address = "";
numpeople = 0;
income =0;
}
和有关测试类代码是:
Household temp;
string n;
int i;
cout<< "Please enter the name, press enter, then the income of the housen";
getline(cin, n);
cin >> i;
myWard.removeHouse(temp(n, n, i, i));
break;
错误消息是
Error: no match for call to '(Household) (std:string&, std::string&, int&, int&)'
我真的不明白为什么会发生这种情况,因为我的家用构造函数确实具有所有这些参数。我可能缺少一些明显的东西,但对我来说并不那么明显。这是我第一次与C 合作。
编辑:Removehouse和Myward在此问题中无关紧要,但是我添加了临时代码。问题是代码
temp(n,n,i,i)
多数民众赞成在错误。
预先感谢!
您用默认构造函数 temp 对象初始化它使用另一个构造函数 ..这就是问题。
Household temp; <-- initialized with DEFAULT constructor
string n;
int i;
cout<< "Please enter the name, press enter, then the income of the housen";
getline(cin, n);
cin >> i;
myWard.removeHouse(temp(n, n, i, i)); <-- RE-initialize?? Bad idea..
break;
用所需的构造函数(我想是4个arguments On)初始化对象,然后使用对象。
另外:GCC可能正在使用,它警告您,通过一组参数,没有解决方案。
这样的事情可能有效:
string n;
int i;
// do initialize n and i to something meaningful here
Household temp(n, n, i, i);
cout<< "Please enter the name, press enter, then the income of the housen";
getline(cin, n);
cin >> i;
myWard.removeHouse(temp);
break;
我一无所知,但对程序的行为一无所知。
表达式 temp(n, n, i, i)
不会"重新呼叫"构造函数,这是您似乎正在尝试执行的操作。相反,该表达式试图在temp
对象上调用函数应用程序operator()
。您尚未为您的班级提供这样的操作员,因此编译器正确地抱怨。
您无法重新打电话给对象的构造函数。但是,您 can 做的是等待声明对象,直到您真正需要它,然后将其调用构造函数:
:{
cout<< "Please enter the name, press enter, then the income of the housen";
getline(cin, n);
cin >> i;
Household temp(n, n, i, i);
myWard.removeHouse(temp);
}
break;
我添加了牙套,因为此代码看起来是switch
语句的一部分。在switch
块中间声明变量,因为该代码现在将与temp
一起使用,可能会导致问题。支架限制了新变量的范围。
我相信这可能是因为您有两个构造函数,只有参考文献而有所不同。
编译器认为它是模棱两可的,不知道要拨打哪个构造函数。因此"无匹配"。
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 错误:与呼叫"(家庭)"不匹配