【发布时间】:2018-03-04 20:58:43
【问题描述】:
我收到错误:[Error] no matching function for call to 'subjects::subjects(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
我该如何解决这个问题?或者在这种情况下我不应该使用向量,忽略友元函数,因为我们需要使用它
【问题讨论】:
-
好吧,你没有一个带参数的构造函数——那么你希望
new subjects(a.subjectName, a.lectName,a.lectSurname,a.credits, a.studentnum)如何工作? -
subjects::subjects不接受参数,根据标题 -
您的问题标题与内容无关。
-
首先,将您的代码简化为 MCVE。这不仅是让您对主题提出问题所必需的,还可以帮助您理解问题。
标签: c++