简体   繁体   English

C ++错误:没有匹配的函数可调用

[英]C++ error: no matching function for call to

Here's the error I got : "no matching function for call to 'MemberForTest::MemberForTest'..." 这是我得到的错误:“没有匹配的函数来调用'MemberForTest :: MemberForTest'...”

Here is the code: 这是代码:

#include "Base.h"
#include "Date.h"

class MemberForTest: public tp::Base
{
public:
    MemberForTest(std::string& name, std::string& firstname,
            Date& birthday, std::string& telephone);

};




class Member
{
public:
    Member() :
        perso("Hall", "roger", (12,12,1990), "(999) 444-4545")
    {
    };

    MemberForTest perso;
};

Base is an abstract class. Base是一个抽象类。 My teacher used the same approach to get to the base class constructor and he does not need to create a function (MemberForTest::MemberForTest). 我的老师使用相同的方法来访问基类的构造函数,而他不需要创建一个函数(MemberForTest :: MemberForTest)。 Even if I create the function, the error is still there. 即使创建函数,该错误仍然存​​在。 Can you help me? 你能帮助我吗?

Also, I probably have to pass an object date (Date date(12,12,1990)) rather than (12,12,1990) as parameters. 另外,我可能必须传递对象日期(Date date(12,12,1990))而不是(12,12,1990)作为参数。 How can I do that? 我怎样才能做到这一点?

The expression that you use to initialize Date , namely 用于初始化Date的表达式,即

(12,12,1990)

is a comma expression that evaluates to an int (specifically, 1990 , the last number in the chain of comma-separated numbers). 是一个以int表示的逗号表达式(具体是1990 ,是逗号分隔数字链中的最后一个数字)。 The error indicates that an int is incompatible with a Date . 该错误表明intDate不兼容。

Modify your code to construct a Date object instead. 修改您的代码以构造Date对象。 Unfortunately, you cannot do it inline, because MemberForTest constructor takes the Date parameter by non-constant reference. 不幸的是,您不能内联完成此操作,因为MemberForTest构造函数通过非恒定引用获取Date参数。 If you change the constructor to take parameters by constant reference, like this 如果您更改构造函数以通过常量引用获取参数,例如

MemberForTest(const std::string& name, const std::string& firstname,
        const Date& birthday, const std::string& telephone);

you should be able to do this: 您应该能够做到这一点:

Member() :
    perso("Hall", "roger", Date(12,12,1990), "(999) 444-4545")
{
};

When a parameter is declared as a reference to non-const, such as std::string& name , its argument needs to be an lvalue (a named object). 当参数被声明为对非const的引用时,例如std::string& name ,其参数必须是左值(命名对象)。 You're passing string literals - these are arrays of characters, not std::string , and need to be converted. 您正在传递字符串文字-这些是字符数组,而不是std::string ,需要进行转换。 The result of a conversion is a temporary object, an rvalue. 转换的结果是一个临时对象,即右值。 The language rule says that these cannot be passed to a reference to non-const. 语言规则说,这些不能传递给对非常量的引用。

You need to either declare the constructor to take const std::string& (since you won't be changing the arguments), or create an object of std::string type first: 您需要声明构造函数以采用const std::string& (因为您将不会更改参数),或者首先创建一个std::string类型的对象:

void foo(std::string& s);
std::string str;
foo(str);

Also, (12,12,1990) does not create a temporary Date object (for that you'd need to use functional style cast - Date(12,12,1990) ). 同样, (12,12,1990)不会创建临时Date对象(为此,您需要使用功能样式Date(12,12,1990) )。 It's a parenthesized expression, using the comma operator , that evaluates the left hand operands, discards them, and return the rightmost operand (integer literal 1990 ). 这是一个带括号的表达式,使用逗号运算符计算左手操作数,将其丢弃,然后返回最右边的操作数(整数常量1990 )。

And the same applies as with string parameters, you need to take const Date& . string参数一样,您需要使用const Date&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM