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

标签 c++

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

我正在开发一个程序,该程序涉及一个名为“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/

相关文章:

c++ - operator= 的值参数导致奇怪的编译错误

c++ - timegm 跨平台

c++ - 英特尔 AVX : 256-bits version of dot product for double precision floating point variables

c++ - 为具有许多变体的类存储默认值的推荐方法是什么?

c++ - 通过 Alpha-Beta 修剪迭代加深 Negamax

c++ - 如何将字符串复制到指针数组的元素?

C++:处理许多配置变量的设计建议

c++ - c++中私有(private)复制构造函数有什么用

java - 将Java代码转换为C++

c++ - 如何使用 gtest 设置测试名称