调用向量没有匹配函数

No matching function for call to vector

本文关键字:函数 向量 调用      更新时间:2023-10-16

我收到错误:[错误]没有匹配函数来调用"主题::主题(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 个时,默认值将用于其余参数。