Home > Net >  C : Calling a constructor from a class inside another class
C : Calling a constructor from a class inside another class

Time:03-15

I am working on a code which contains three classes: date, note, and student.

Date is made up of: int, int, int

Note is made up of: string, double, int

In ex3.h:

    class student;

     class date {
    private:
    //---------declaration of attributes-----
        int j ; // day
        int m ; // month
        int a ; //year

    // --------------
    public:
            date( int , int , int ) ; 
            date( const date & );
friend class student;
};

class note {
    private:
    //---------declaration of attributes-----
        string matiere ;
            double n ; // grade
            int coef ; //coefficient
    
        // --------------
        public:
                note( string , double , int ) ;
    friend class student;

    };

note::note(string mat , double no, int c ){matiere=mat;n=no;coef=c;}

date::date(int a1 , int b , int c){j=a1;m=b;a=c;}

Date and note work fine.

I want student to be made up of: string , string, date, note , note, note

Here is what I have written:

        class student{
        private:
        //---------declaration of attributes-----
            string nom ; // surname
            string prenom ; // first name
            date ddn ; //date of birth
            note n1;
            note n2;
            note n3;
            double mean;
    
        // --------------
        public:
                student( string , string, date, note , note, note ) ;
    
    student::student(string nomi, string prenomi, date di, note nte1, note nte2, note nte3 ){
nom=nomi;prenom=prenomi;ddn=di;n1=nte1;n2=nte2;n3=nte3;}

I tried to create a student with the following, in ex3.cpp:

date d1(12,10,2000);

note nt1("Math",20,2);
note nt2("Math",20,2);
   

 note nt3("English",19.5,3);    

student e1("Appleseed","Johnny",d1,nt1,nt2,nt3);

I get the following errors for the last line: "error: no matching function for call to 'date::date()' "

"error: no matching function for call to 'note::note()' " (twice, since note is in the function twice)

I have tried changing the arguments of the last line, the arguments of the constructor... I'm out of ideas on how to make the student class functional. Can anyone help me?

Thanks!

I have copied bits of my code, so I hope nothing is missing.

Original code is not English, so I translated it the best I could.

PS: I am new to stack overflow, so please bear with me as I ask my first question ^^'

CodePudding user response:

When you create an object of student type, the members of it are created with default constructors before the constructor of the student starts execution. So, the compiler tries to call the default constructor of the date type object, and then in the body of the student's constructor, it makes a call to the copy assignment. The same problem applies to the note objects. By defining a custom constructor, you deleted the default constructor of those classes.

The way to solve it is to use an initializer list. The code below would solve your problem.

student::student(string nomi, string prenomi, date di, note nte1, note nte2, note nte3)
: ddn(di), n1(nte1), n2(nte2), n3(nte3)
{
    nom     = nomi;
    prenom  = prenomi;
}

You can also explicitly say that you need the default constructor by typing the following in each class.

class date {
public:
    date() = default;

    /** ... **/
};

class note {
public:
    note() = default;

    /** ... **/
};

By the way, the indentation and the organization of your code make it hard to read. I suggest you work clean to learn more efficiently.

  • Related