错误:与呼叫"(家庭)"不匹配

Error: no match for call to '(Household)

本文关键字:家庭 不匹配 错误 呼叫      更新时间:2023-10-16

因此,我基本上有一个错误,对我来说是没有意义的。我已经尝试了所有事情,但似乎没有任何作用,所以我认为你们将能够帮助我。顺便说一句,这是我在本网站上的第一篇文章。

我正在研究一个涉及一个名为" 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一起使用,可能会导致问题。支架限制了新变量的范围。

我相信这可能是因为您有两个构造函数,只有参考文献而有所不同。

编译器认为它是模棱两可的,不知道要拨打哪个构造函数。因此"无匹配"。