继承的类C++,虚拟函数无法实现

发布于 2025-02-08 14:43:23 字数 1833 浏览 0 评论 0原文

我正在尝试使用返回Tuition_Fees+Library_fines的方法来创建一个班级学生的毕业生(后者是私有的,我们可以通过在父级中公开的get和set函数访问它)。我收到以下消息:“缺失的VTable通常意味着第一个非内部虚拟成员函数没有定义。”这证明有一个虚拟函数无法实现。这是我的标题,实施和主代码,我找不到错误。有什么建议可以解决吗?谢谢 !

#include "Student.hpp"


class GraduateStudent : public Student {
private:
        double library_fines;
public:
    GraduateStudent();
    GraduateStudent(std::string name, double fines, double fees, bool fullTime);
    bool fullTime;

    virtual double MoneyOwed() const;
    virtual ~GraduateStudent() {} ;
};


#include "GraduateStudent.hpp"
#include "Student.hpp"
#include <iostream>


GraduateStudent::GraduateStudent() : Student()
{
     bool fullTime; 
}



double GraduateStudent::MoneyOwed() const
{
       return tuition_fees + GetLibraryFines();
}

int main(int argc, char* argv[])
{
      Student* stu1= new GraduateStudent;
      Student* stu2= new GraduateStudent;

//computing the owes
      stu1->tuition_fees= 1000.0;
      stu1->SetLibraryFines(10);
      stu2->SetLibraryFines(1500);
      stu2->tuition_fees= 1000.0;


std::cout << "Fees for Student 1=" << stu1-> MoneyOwed()<<"\n";
std::cout << "Fees for Student 2=" << stu2-> MoneyOwed()<<"\n";


delete stu1;
delete stu2;



return 0;
}

当我测试父班时,一切正常。但是我更喜欢在这里分享父班的标题:

#include <string>
#include <iostream>

class Student {
public:
    Student();
    Student(std::string name, double fines, double fees);

    std::string name;
    double tuition_fees;
    virtual double MoneyOwed() const;
    virtual ~Student() {};

    void SetLibraryFines(double amount);
    double GetLibraryFines() const;
        friend std::ostream& operator<<(std::ostream& out, Student const& obj);

    private:
        double library_fines;
    };

I am trying to create an inherited class GraduateStudent of the class Student with a method that returns the tuition_fees+library_fines (this latter is private and we get access to it via Get and Set functions that are public in the parent class). I get the following message: "a missing vtable usually means the first non-inline virtual member function has no definition." Which proves that there's a virtual function that cannot be implemented. Here's my header, implementation and main codes, I cannot spot my mistake. Any suggestions to fix that ? Thank you !

#include "Student.hpp"


class GraduateStudent : public Student {
private:
        double library_fines;
public:
    GraduateStudent();
    GraduateStudent(std::string name, double fines, double fees, bool fullTime);
    bool fullTime;

    virtual double MoneyOwed() const;
    virtual ~GraduateStudent() {} ;
};


#include "GraduateStudent.hpp"
#include "Student.hpp"
#include <iostream>


GraduateStudent::GraduateStudent() : Student()
{
     bool fullTime; 
}



double GraduateStudent::MoneyOwed() const
{
       return tuition_fees + GetLibraryFines();
}

int main(int argc, char* argv[])
{
      Student* stu1= new GraduateStudent;
      Student* stu2= new GraduateStudent;

//computing the owes
      stu1->tuition_fees= 1000.0;
      stu1->SetLibraryFines(10);
      stu2->SetLibraryFines(1500);
      stu2->tuition_fees= 1000.0;


std::cout << "Fees for Student 1=" << stu1-> MoneyOwed()<<"\n";
std::cout << "Fees for Student 2=" << stu2-> MoneyOwed()<<"\n";


delete stu1;
delete stu2;



return 0;
}

When I test for the parent class, everything works perfectly. But I prefer sharing the header of the parent class here:

#include <string>
#include <iostream>

class Student {
public:
    Student();
    Student(std::string name, double fines, double fees);

    std::string name;
    double tuition_fees;
    virtual double MoneyOwed() const;
    virtual ~Student() {};

    void SetLibraryFines(double amount);
    double GetLibraryFines() const;
        friend std::ostream& operator<<(std::ostream& out, Student const& obj);

    private:
        double library_fines;
    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文