提问人:weuoimi 提问时间:9/5/2023 最后编辑:weuoimi 更新时间:9/5/2023 访问量:68
为什么我在 C++ 代码中收到“没有匹配的构造函数错误”?[复制]
Why am I getting 'No matching constructor error' in C++ code? [duplicate]
问:
我最近开始学习 C++,使用带有 lsp 模式的 emacs。我在代码中有这些错误:
没有用于初始化“Student[5]”的匹配构造函数
“Student”不是指值
搞不清问题出在哪里......
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#define STUDENT_ARRAY_SIZE 5
typedef struct Date
{
int day;
int month;
int year;
} date;
date parse_date (const std::string &date_string)
{
date input;
std::istringstream stream(date_string);
char delimeter = '/';
stream >> input.day >> delimeter >> input.month >> delimeter >> input.year;
return input;
};
typedef struct FullName
{
std::string name;
std::string surname;
std::string secondary_name;
} full_name;
full_name parse_name (const std::string &name_string)
{
full_name input;
std::istringstream stream(name_string);
char delimeter = ' ';
stream >> input.name >> delimeter >> input.surname >> input.secondary_name;
return input;
}
class Student
{
private:
full_name name;
date birth_day;
std::string telephone_number;
std::string address;
std::string faculty;
int course;
public:
// Constructor
Student(full_name name, date birth_day, std::string
telephone_number, std::string address, std::string faculty, int
course)
{
this->name = name;
this->birth_day = birth_day;
this->telephone_number = telephone_number;
this->address = address;
this->faculty = faculty;
this->course = course;
}
// Getter methods
full_name getFullName()
{
return name;
}
date getBirthDay()
{
return birth_day;
}
std::string getTelephoneNumber()
{
return telephone_number;
}
std::string getAddress()
{
return address;
}
std::string getFaculty()
{
return faculty;
}
int getCourse()
{
return course;
}
// Setter methods
void setFullName(full_name name)
{
this->name = name;
}
void setBirthDay(date birth_day)
{
this->birth_day = birth_day;
}
void setTelephoneNumber(std::string telephone_number)
{
this->telephone_number = telephone_number;
}
void setAddress(std::string address)
{
this->address = address;
}
void setFaculty(std::string faculty)
{
this->faculty = faculty;
}
void setCourse(int course)
{
this->course = course;
}
void show();
};
void Student::show()
{
std::cout << "Name: " << name.name << " " << name.surname << " "
<< name.secondary_name << std::endl;
std::cout << "Birthday: " << birth_day.day << "/" <<
birth_day.month << "/" << birth_day.year << std::endl;
std::cout << "Phone Number: " << telephone_number << std::endl;
std::cout << "Address: " << address << std::endl;
std::cout << "Faculty: " << faculty << std::endl;
std::cout << "Course: " << course << std::endl;
}
int
main (void)
{
Student *db = new Student[STUDENT_ARRAY_SIZE];
db[0] = Student student(parse_name("Name Surname SecondaryName"),
parse_date("12/04/2003"), "+123123123", "Sporty street 8",
"MMF", 3);
delete[] db;
return 0;
}
尝试使用我所有的知识和 gdb,但没有成功。期待您对如何使其工作以及如何使其更好的建议。
答:
0赞
Ahmed AEK
9/5/2023
#1
Student *db = new Student[STUDENT_ARRAY_SIZE];
这是构造(和初始化)一个由 5 名学生组成的数组,它必须调用他们的默认构造函数(一个没有参数的构造函数)。您没有定义默认构造函数,因此您应该定义一个,它应该使用默认值初始化成员,或者通过将其设置为等于 来默认为“不做任何特殊的事情”,默认构造成员。(构造空字符串)default
class Student
{
...
public:
Student() = default;
...
}
评论
typedef struct Date ... date
Student
this