Home > Net >  Why do I get the error "use of deleted function 'class : : class()"
Why do I get the error "use of deleted function 'class : : class()"

Time:10-24

#include <iostream>
using namespace std;

class student
{
protected:
    string name;
    int roll;
    int age;
public:
    student(string n, int r, int a)
    {
        name = n;
        roll = r;
        age = a;
    }
};

class test : public student
{
protected:
    int sub[5];
public:
    void marks()
    {
        cout << "Enter marks in 5 subjects: " << endl;
        cin >> sub[0] >> sub[1] >> sub[2] >> sub[3] >> sub[4];
    }
    void display()
    {
        cout << "Name : " << name << "\nRoll number : " << roll << "\nAge: " << age << endl;
        cout << "Marks in 5 subjects : " << sub[0] << ", " << sub[1] << ", " << sub[2] << ", " << sub[3] << ", " << sub[4] << endl;
    }
};

class sports
{
protected:
    int sportmarks;
public:
    sports(int sm)
    {
        sportmarks = sm;
    }
};

class result : public test, public sports
{
    int tot;
    float perc;
public:
    void calc()
    {
        tot = sportmarks;
        for(int i = 0; i < 5; i  )
            tot = tot   sub[i];
        perc = (tot / 600.0) * 100;
        cout << "Total: " << tot << "\nPercentage: " << perc << endl;
    }
};

int main()
{
    student ob1("Name", 781, 19);
    sports ob2(78);
    result ob;
    ob.marks();
    ob.display();
    ob.calc();
}

I've been asked to use parameterized constructors to do this problem and notice I get these errors when I use constructors. Sorry if this post is inconvenient to read. In what way can I run this code while creating objects from parameterized constructors?

use of deleted function 'result::result()'
use of deleted function 'test::test()'.
no matching function for call to 'student::student()'
no matching function for call to 'sports::sports()'

CodePudding user response:

student and sports have user-defined constructors, so the compiler does not generate default constructors for them.

test and result have no user-defined constructors, so the compiler will generate default constructors for them. However, since student and sports have no default constructors, the generated default constructors are marked delete'd as they are not usable. That is why you get errors.

To make your main() compile as-is, you need to define your own default constructors for test and result which pass explicit parameter values to their base classes, eg:

#include <iostream>
using namespace std;

class student
{
protected:
    string name;
    int roll;
    int age;
public:
    student(string n, int r, int a)
    {
        name = n;
        roll = r;
        age = a;
    }
};

class test : public student
{
protected:
    int sub[5];
public:
    test() : student("", 0, 0) {}

    void marks()
    {
        cout << "Enter marks in 5 subjects: " << endl;
        cin >> sub[0] >> sub[1] >> sub[2] >> sub[3] >> sub[4];
    }

    void display()
    {
        cout << "Name : " << name << "\nRoll number : " << roll << "\nAge: " << age << endl;
        cout << "Marks in 5 subjects : " << sub[0] << ", " << sub[1] << ", " << sub[2] << ", " << sub[3] << ", " << sub[4] << endl;
    }
};

class sports
{
protected:
    int sportmarks;
public:
    sports(int sm)
    {
        sportmarks = sm;
    }
};

class result : public test, public sports
{
    int tot;
    float perc;
public:
    result() : test(), sports(0) {}

    void calc()
    {
        tot = sportmarks;
        for(int i = 0; i < 5; i  )
            tot = tot   sub[i];
        perc = (tot / 600.0) * 100;
        cout << "Total: " << tot << "\nPercentage: " << perc << endl;
    }
};

int main()
{
    student ob1("Name", 781, 19);
    sports ob2(78);
    result ob;
    ob.marks();
    ob.display();
    ob.calc();
}

Online Demo

However, this is not very useful, so I would suggest tweaking result's constructor to take the student and sports objects you have already created beforehand, and pass them to the base class copy constructors, which the compiler will generate for you, eg:

#include <iostream>
using namespace std;

class student
{
protected:
    string name;
    int roll;
    int age;
public:
    student(string n, int r, int a)
    {
        name = n;
        roll = r;
        age = a;
    }
};

class test : public student
{
protected:
    int sub[5];
public:
    test(const student &s) : student(s) {}

    void marks()
    {
        cout << "Enter marks in 5 subjects: " << endl;
        cin >> sub[0] >> sub[1] >> sub[2] >> sub[3] >> sub[4];
    }

    void display()
    {
        cout << "Name : " << name << "\nRoll number : " << roll << "\nAge: " << age << endl;
        cout << "Marks in 5 subjects : " << sub[0] << ", " << sub[1] << ", " << sub[2] << ", " << sub[3] << ", " << sub[4] << endl;
    }
};

class sports
{
protected:
    int sportmarks;
public:
    sports(int sm)
    {
        sportmarks = sm;
    }
};

class result : public test, public sports
{
    int tot;
    float perc;
public:
    result(const student &s, const sports &sp) : test(s), sports(sp) {}

    void calc()
    {
        tot = sportmarks;
        for(int i = 0; i < 5; i  )
            tot = tot   sub[i];
        perc = (tot / 600.0) * 100;
        cout << "Total: " << tot << "\nPercentage: " << perc << endl;
    }
};

int main()
{
    student ob1("Name", 781, 19);
    sports ob2(78);
    result ob(ob1, ob2);
    ob.marks();
    ob.display();
    ob.calc();
}

Online Demo

  • Related