1

I write a 'Student' class which has two classes called 'Course' and 'Score' as its members.
Now I write a constructor for initialization of 'Student' class and get these errors:
1.missing default argument on parameter 'e'
2.no matching constructor for initialization of 'Student'
3.candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided for class Student

update: After change the class, now I find the problem is in my main function, how to solve this? I give the errors and warnings in the picture.

#include <iostream>
#include <string>
using namespace std;
class Course
{
    friend void readCourse(Course &scourse);
public:
    Course(Course &scourse) : cno(scourse.cno), cname(scourse.cname) { }
    Course(const string &a, const string &b) : cno(a), cname(b) { }
    void course_show();
private:
    string cno;
    string cname;
};
class Score
{
    friend void readScore(Score &sscore);
public:
    Score(Score &sscore) : score(sscore.score) { }
    Score(int a) : score(a) { }                                                                                                                                                                                          
    void score_show();
private:
    int score;
};
class Student
{
    friend void readStudent(Student &student);
public:
    Student(const string a = "", const string b = "", const string c = "", const string d = "",
        Course e, Score f) : sno(a), sname(b), gender(c), grade(d),
            scourse(e), sscore(f) { }
    void student_show();
private:
    string sno;
    string sname;
    string gender;
    string grade;
    Course scourse;
    Score sscore;
};

int main()
{
    Student s1;
    Student s2;
    readStudent(s1);
    readStudent(s2);
    s1.student_show();
    s2.student_show();
    return 0;
}

errors and warnings

CaSdm
  • 27
  • 4
  • 1
    Possible duplicate of [Missing default argument - compiler error](https://stackoverflow.com/questions/5740296/missing-default-argument-compiler-error) – TrebledJ Apr 09 '19 at 09:55
  • Paste your errors/warnings as plain text, and not in the form of images. – CinCout Apr 09 '19 at 11:34
  • You don't need to define copy constructors that just memberwise copy, and doing so suppresses the implicit move constructor you would otherwise get. – Caleth Apr 09 '19 at 12:31
  • you don't need to set default value as "" for strings. Since you can always add multiple c'tors as Student(Cource a, Score b) and so on... – Destructor Apr 09 '19 at 15:24

2 Answers2

3

Parameters with default values should always be placed at the end of the parameter list.

Hence,

Student(const string a = "", const string b = "", const string c = "", const string d = "",
        Course e, Score f)

should be

Student(Course e, Score f, const string a = "", const string b = "", const string c = "", const string d = "")
CinCout
  • 9,486
  • 12
  • 49
  • 67
  • Thank you first! However it still cannot compile right for the third reason – CaSdm Apr 09 '19 at 10:00
  • So it may be something wrong in my main function? I will update the code and show you my main function, please see again. – CaSdm Apr 09 '19 at 11:32
  • See [this comment](https://stackoverflow.com/questions/55589875/how-to-write-a-constructor-for-a-class-who-includes-another-class/55589952?noredirect=1#comment97880196_55590032) by [Dulvin Witharane](https://stackoverflow.com/users/2760267/dulvin-witharane). – CinCout Apr 09 '19 at 11:39
1

Default parameters should be moved to the end of the argument list.

Student(const string a = "", const string b = "", const string c = "", const string d = "", Course e, Score f)

The above must be corrected as,

Student(Course e, Score f, const string a = "", const string b = "", const string c = "", const string d = "")
Destructor
  • 523
  • 1
  • 3
  • 13
  • Thank you! However it still cannot compile right for the third reason T^T – CaSdm Apr 09 '19 at 10:03
  • 1
    You cannot do Student s2; since the default constructor is not there anymore once you define your own constructor. Therefore, Students s2(e, f); atlease. – Destructor Apr 09 '19 at 11:31