我收到错误:[错误]没有匹配函数用于调用'subject :: subject(std :: string&,std :: string&,std :: string&,int&,int&)'。 这是我的代码:
void listInput(subjects a){
// subjects a;
cout<<"Enter the name of the subject"<<endl;
cin>>a.subjectName;
while(a.subjectName.empty()){
cout<<"Error, subject name must not be empty"<<endl;
cin>>a.subjectName;
}
cout<<"Enter the name of lecturer"<<endl;
cin.ignore();
getline(cin, a.lectName);
while(checkName(a.lectName)!=1){
cout<<"Error, invalid input"<<endl;
getline(cin, a.lectName);
}
cout<<"Enter the surname of lecturer"<<endl;
getline(cin, a.lectSurname);
while(checkName(a.lectSurname)!=1){
cout<<"Error, invalid input"<<endl;
getline(cin, a.lectSurname);
}
a.credits=a.enterNumber("Enter the number of credits\n");
a.studentnum=a.enterNumber("Enter the number of students\n");
a.entry.push_back(new subjects(a.subjectName, a.lectName,a.lectSurname,a.credits, a.studentnum));
}
头文件:
#ifndef SUBJECT
#define SUBJECT
#include <string>
#include <vector>
class subjects{
private:
std::string subjectName;
std::string lectName;
std::string lectSurname;
int credits;
int studentnum;
public:
subjects(){
subjectName="";
lectName="";
lectSurname="";
credits=0;
studentnum=0;
}
int userChoice();
int enterNumber(std::string name);
void menu();
friend void listInput(subjects a);
bool checkName(std::string &text);
std::vector<subjects*> entry;
};
#endif
我该如何解决这个问题?或者我应该在这种情况下不使用向量,忽略友元函数,因为我们需要使用它
答案 0 :(得分:2)
您没有获取所有这些参数的构造函数。将另一个构造函数添加到subjects
,其中包含适当的参数,您的代码应该没问题。
答案 1 :(得分:1)
您有一个默认构造函数,但没有构造函数接受四个参数。您可以通过添加默认值为
的构造函数同时使用两者subjects(
const std::string subjectName=""
, const std::string lectName = ""
, const std::string lectSurname=""
, const int credits = 0
, const int studentnum = 0
) : subjectName(subjectName)
, lectName(lectName)
, lectSurname(lectSurname)
, credits(credits)
, studentnum(studentnum)
{
}
此方法允许您使用零,一,二,三,四或五个参数调用subjects
构造函数。当传递的参数数量少于5时,默认值用于其余参数。