调用向量没有匹配函数
No matching function for call to vector
我收到错误:[错误]没有匹配函数来调用"主题::主题(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 creditsn");
a.studentnum=a.enterNumber("Enter the number of studentsn");
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
我该如何解决这个问题?或者在这种情况下我不应该使用向量,忽略朋友函数,因为我们需要使用它
您没有接受所有这些参数的构造函数。 向subjects
添加另一个采用适当参数的构造函数,您的代码应该没问题。
你有一个默认构造函数,但没有构造函数采用四个参数。您可以通过添加具有默认值的构造函数来同时拥有两者:
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 个时,默认值将用于其余参数。
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用向量的template_back函数
- 如何通过派生类函数更改基类中的向量
- 变量没有改变?通过向量的函数调用
- 一对向量构造函数:初始值设定项列表与显式构造
- 在c++中使用向量时,如何调用构造函数和析构函数
- std::sort()函数无法对向量的一部分进行排序
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- C++中函数的向量返回类型引发错误
- 添加存储在向量中的大整数的函数出现问题
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 具有 lambda 函数的函数向量
- 你能用参数制作函数向量吗?
- 在C++中使用成员函数向量时,有没有办法实现协变返回类型
- 在编写 lambda 函数向量时出现隔离错误
- 如何在 boost.process 间共享内存中使用复杂结构"push_back"函数向量
- 是否将std矢量重新分配到默认的构造函数向量是擦除的好方法
- 运行提升for_each中的成员函数向量
- 创建和回推函数向量
- C++,指向函数向量的指针的STL映射