Tom
Tom

Reputation: 31

No matching function for call to vector

I get the error: [Error] no matching function for call to 'subjects::subjects(std::string&, std::string&, std::string&, int&, int&)'. Here is my code:

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));
}

And header file:

#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

How can i fix this? Or should i not use vector in this case, ignore the friend function, as we are required to use it

Upvotes: 1

Views: 474

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

You have a default constructor, but no constructor taking four parameters. You can have both at the same time by adding a constructor with defaults:

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)
{
}

This approach lets you call subjects constructor with zero, one, two, three, four, or five parameters. When the number of passed parameters is fewer than five, the defaults are used for the remaining ones.

Upvotes: 1

Stephen Newell
Stephen Newell

Reputation: 7828

You don't have a constructor that takes all those arguments. Add another constructor to subjects that takes the appropriate parameters and your code should be fine.

Upvotes: 2

Related Questions