啊啊哦哦啦啦 2019-04-20 22:17 采纳率: 0%
浏览 993

c++报错undefined reference to "析构函数",引号里的析构函数是基类的析构函数,声明为了纯虚函数?

这是c++ primer plus第六版中的程序清单14.10,P560页。
一模一样,就是这样报错,也没办法继续写下去了。
自学的,没办法了,不知道找谁问,贼难受!!!

//类声明
#ifndef WORKER_H_
#define WORKER_H_

#include <string>

using std::string;

class Worker{
    private:
        string fullname;
        long id;

    protected:
        virtual void Data() const;
        virtual void Get();

    public:
        Worker() : fullname("no one"), id(0L) {}
        Worker(const string & s, long n) : fullname(s), id(n) {}

        virtual ~Worker() = 0;
        virtual void Set() = 0;
        virtual void Show() const = 0;
};

class Waiter : virtual public Worker
{
    private:
        int panache;

    protected:
        virtual void Data() const;
        virtual void Get();

    public:
        Waiter() : Worker(), panache(0) {}
        Waiter(const string & s, long n, int p = 0) : Worker(s, n), panache(p) {}
        Waiter(const Worker & wk, int p = 0) : Worker(wk), panache(p) {}

        void Set();
        void Show() const;
};

#endif
//实现文件
#include <iostream>
#include "worker.h"

using namespace std;

//Worker 方法
void Worker::Data() const
{
    cout << "名字:" << fullname << endl;
    cout << "工号:" << id << endl;
}

void Worker::Get()
{
    getline(cin,fullname);
    cout << "请输入工号:";
    cin >> id;
    while (cin.get() != '\n')   continue;
}




//Waiter 方法
void Waiter::Data() const
{
    cout << "等级:" << panache << endl;
}

void Waiter::Get()
{
    cout << "请输入等级:";
    cin >> panache;
    while (cin.get() != '\n')   continue;
}

void Waiter::Set()
{
    cout << "请输入服务员的姓名:" << endl;
    Worker::Get();
    Get();
}

void Waiter::Show() const
{
    Worker::Data();
    Data();
}
  • 写回答

3条回答

  • BugM 2020-03-21 10:14
    关注

    纯虚析构函数在c++中是合法的,但是在使用的时候有一个额外的限制:必须为纯虚析构函数提供一个函数体。你的实现文件并没有给worker类的纯虚函数定义函数体,因此会有报错

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况