gpt4 book ai didi

c++ - 错误 : no match for call to '(Household)

转载 作者:行者123 更新时间:2023-11-28 02:58:23 24 4
gpt4 key购买 nike

所以基本上我有一个对我来说毫无意义的错误。我已经尝试了一切,但似乎没有任何效果,所以我想你们可以帮助我。顺便说一下,这是我在此网站上的第一篇文章。

我正在开发一个程序,该程序涉及一个名为“household.cc, h”的类和一个测试程序。

这是Household.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);

这是我有问题的 Household.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 house\n";
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&)'

我真的不明白为什么会这样,因为我的 Household 构造函数确实具有所有这些参数。我可能遗漏了一些明显的东西,但对我来说并不那么明显。这也是我第一次使用 C++。

编辑:removeHouse 和 myWard 与这个问题无关,但我添加了临时代码。问题是代码

temp(n,n,i,i) 

这就是错误所在。

提前致谢!

最佳答案

您使用默认构造函数初始化了temp对象,然后稍后您尝试使用另一个构造函数<重新初始化它/strong>.. 这就是问题所在。

Household temp;  <-- initialized with DEFAULT constructor
string n;
int i;

cout<< "Please enter the name, press enter, then the income of the house\n";
getline(cin, n);
cin >> i;
myWard.removeHouse(temp(n, n, i, i)); <-- RE-initialize?? Bad idea..
break;

请使用您想要的构造函数(我想是 4 个参数的构造函数)初始化对象,然后使用该对象。

此外:可能正在使用 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 house\n";
getline(cin, n);
cin >> i;
myWard.removeHouse(temp);
break;

不过我对其他功能和程序的行为一无所知。

关于c++ - 错误 : no match for call to '(Household),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21490903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com