C++;带参数列表的构造函数 我熟悉C++中的构造函数,想知道为什么我的C++编译器找不到带有参数列表的构造函数。 #include <cstdio> #include <string> const int defaultAge = 0; const std::string unknown = "unknown"; class Patient { public: int age; std::string dob; std::string name; public: Patient(); // Default Constructor Patient(int &years, std::string &birthdate, std::string &aliase); // Argument-List Constructor void print(); }; Patient::Patient() : age(defaultAge), dob(unknown), name(unknown) { puts("Patient information from default consturctor:"); } Patient::Patient(int &years, std::string &birthdate, std::string &aliase) : age(years), dob(birthdate), name(aliase) { puts("Patient information from copy consturctor:"); } void Patient::print() { printf(" Name - %d\n DOB - %s\n Name - %s\n", age, dob.c_str(), name.c_str()); } int main(void) { Patient p0; p0.print(); Patient p1(40, "August 11, 1980", "John Doe"); p1.print(); return 0; } #包括 #包括 const int defaultAge=0; const std::string unknown=“unknown”; 班级病人{ 公众: 智力年龄; std::字符串dob; std::字符串名; 公众: Patient();//默认构造函数 患者(整数和年份,std::string和生日,std::string和别名);//参数列表构造函数 作废打印(); }; 患者::患者():年龄(默认年龄)、出生日期(未知)、姓名(未知){ puts(“来自默认构造函数的患者信息:”); } 患者::患者(整数和年份,std::字符串和出生日期,std::字符串和别名) :年龄(岁)、出生日期(出生日期)、姓名(别名){ puts(“来自复制构造函数的患者信息:”); } 作废患者::打印(){ printf(“姓名-%d\n出生日期-%s\n姓名-%s\n”、年龄、出生日期、出生日期、出生日期、出生日期、出生日期); } 内部主(空){ 患者p0; p0.print(); 患者p1(40名,1980年8月11日,John Doe); p1.print(); 返回0; }

C++;带参数列表的构造函数 我熟悉C++中的构造函数,想知道为什么我的C++编译器找不到带有参数列表的构造函数。 #include <cstdio> #include <string> const int defaultAge = 0; const std::string unknown = "unknown"; class Patient { public: int age; std::string dob; std::string name; public: Patient(); // Default Constructor Patient(int &years, std::string &birthdate, std::string &aliase); // Argument-List Constructor void print(); }; Patient::Patient() : age(defaultAge), dob(unknown), name(unknown) { puts("Patient information from default consturctor:"); } Patient::Patient(int &years, std::string &birthdate, std::string &aliase) : age(years), dob(birthdate), name(aliase) { puts("Patient information from copy consturctor:"); } void Patient::print() { printf(" Name - %d\n DOB - %s\n Name - %s\n", age, dob.c_str(), name.c_str()); } int main(void) { Patient p0; p0.print(); Patient p1(40, "August 11, 1980", "John Doe"); p1.print(); return 0; } #包括 #包括 const int defaultAge=0; const std::string unknown=“unknown”; 班级病人{ 公众: 智力年龄; std::字符串dob; std::字符串名; 公众: Patient();//默认构造函数 患者(整数和年份,std::string和生日,std::string和别名);//参数列表构造函数 作废打印(); }; 患者::患者():年龄(默认年龄)、出生日期(未知)、姓名(未知){ puts(“来自默认构造函数的患者信息:”); } 患者::患者(整数和年份,std::字符串和出生日期,std::字符串和别名) :年龄(岁)、出生日期(出生日期)、姓名(别名){ puts(“来自复制构造函数的患者信息:”); } 作废患者::打印(){ printf(“姓名-%d\n出生日期-%s\n姓名-%s\n”、年龄、出生日期、出生日期、出生日期、出生日期、出生日期); } 内部主(空){ 患者p0; p0.print(); 患者p1(40名,1980年8月11日,John Doe); p1.print(); 返回0; },c++,constructor,arguments,pass-by-reference,move-constructor,C++,Constructor,Arguments,Pass By Reference,Move Constructor,我在尝试编译代码时遇到以下错误: 我使用Apple clang 11.0.0版作为编译器您将参数声明为对非常量的左值引用,它不能绑定到右值,如40(这是一个int文本)、“August 11,1980”和“John Doe”(它们是字符串文字,将作为临时值隐式转换为std::string,它们是右值) 您可以让它们左值引用const(对于声明和定义),例如 或者对于int只需按值传递即可 Patient(int years, const std::string &birthdate,

我在尝试编译代码时遇到以下错误:


我使用Apple clang 11.0.0版作为编译器

您将参数声明为对非常量的左值引用,它不能绑定到右值,如
40
(这是一个
int
文本)、
“August 11,1980”
“John Doe”
(它们是字符串文字,将作为临时值隐式转换为
std::string
,它们是右值)

您可以让它们左值引用const(对于声明和定义),例如

或者对于
int
只需按值传递即可

Patient(int years, const std::string &birthdate, const std::string &aliase);
//      ^^^        ^^^^^                         ^^^^^

您将参数声明为对非常量的左值引用,而非常量不能绑定到右值,如
40
(是
int
文字)、
“1980年8月11日”
“John Doe”
(它们是字符串文字,将作为临时值隐式转换为
std::string
,它们是右值)

您可以让它们左值引用const(对于声明和定义),例如

或者对于
int
只需按值传递即可

Patient(int years, const std::string &birthdate, const std::string &aliase);
//      ^^^        ^^^^^                         ^^^^^

宋元耀指出了代码的问题。一个好的替代方法是先传递值,然后移动:

Patient::Patient(int &years, std::string birthdate, std::string aliase) 
    : age(years), dob(std::move(birthdate)), name(std::move(aliase))
{
    puts("Patient information from copy consturctor:");
}
在您的原始代码(或宋元耀的修复)中,每个字符串内容分配两次,而在这个版本中,有一次分配和一次移动


根据经验,在函数要存储参数副本的任何时候都要传递值。

宋元耀指出了代码的问题。一个好的替代方法是传递值,然后移动:

Patient::Patient(int &years, std::string birthdate, std::string aliase) 
    : age(years), dob(std::move(birthdate)), name(std::move(aliase))
{
    puts("Patient information from copy consturctor:");
}
在您的原始代码(或宋元耀的修复)中,每个字符串内容分配两次,而在这个版本中,有一次分配和一次移动


根据经验,在函数要存储参数副本的任何时候,都要按值传递。

为什么要按引用传递
int
?只需按值传递即可。错误应该以文本而不是图像的形式发布,但问题可能在于
“John Doe”
属于
const char[9]类型
,它将被转换为一个临时的
std::string
值,以将其传递给构造函数。但是,构造函数接受一个非常量字符串引用,该引用无法绑定到临时对象。请将构造函数更改为将其两个字符串参数设置为
const string&
,或者将它们设置为
string
(无引用)。为什么要通过引用传递
int
?只需通过值传递即可。错误应该以文本而不是图像的形式发布,但问题在于
“John Doe”
的类型是
常量字符[9]
,它将被转换为一个临时的
std::string
值,以将其传递给构造函数。但是,构造函数接受一个非常量字符串引用,该引用无法绑定到临时对象。请将构造函数更改为将其两个字符串参数设置为
const string&
,或者将它们设置为
string
(无参考资料)。