【问题标题】:Why do I get the error "use of deleted function 'class : : class()"为什么我会收到错误“使用已删除的函数'class :: class()”
【发布时间】:2021-12-10 03:30:21
【问题描述】:
#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();
}

有人要求我使用参数化构造函数来解决这个问题,并注意到我在使用构造函数时遇到了这些错误。如果这篇文章不方便阅读,请见谅。从参数化构造函数创建对象时,我可以通过什么方式运行此代码?

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()'

【问题讨论】:

  • 在您的main() 中有result ob;。这是默认构造的请求。 class result 根本没有构造函数。在这种情况下,编译器会为您生成一个默认构造函数。它构建调用所有基类和所有成员的默认构造函数。所以,我们到了。

标签: c++ inheritance constructor


【解决方案1】:

studentsports 具有用户定义的构造函数,因此编译器不会为它们生成默认构造函数。

testresult 没有用户定义的构造函数,因此编译器将为它们生成默认构造函数。但是,由于studentsports 没有默认构造函数,因此生成的默认构造函数被标记为delete'd,因为它们不可用。这就是你得到错误的原因。

要使您的main() 按原样编译,您需要为testresult 定义自己的默认构造函数,它们将显式参数值传递给它们的基类,例如:

#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

但是,这不是很有用,所以我建议调整 result 的构造函数以获取您事先已经创建的 studentsports 对象,并将它们传递给基类复制构造函数,这编译器会为你生成,例如:

#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

【讨论】:

  • 非常感谢。我现在知道了。我不知道你可以这样通过他们。当我们收到错误“no matching function for call to”时,我还能问吗?
  • @Aniki 我不确定我明白你在问什么。
  • 抱歉不清楚。我的意思是,出现“无匹配功能”错误的原因/条件是什么。但我想我现在明白了。谢谢你。并为额外的 cmets 感到抱歉。
猜你喜欢
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 2013-04-05
  • 2014-04-11
  • 2020-11-08
  • 2019-10-05
相关资源
最近更新 更多