【问题标题】:Undefined reference to vtable (Inheritance)对 vtable 的未定义引用(继承)
【发布时间】:2016-05-18 12:18:06
【问题描述】:

我从抽象类继承有问题。

抽象类-头文件:

    #ifndef CLIENT_H
    #define CLIENT_H
    #include "country.h"
    #include "currency.h"
    #include "item.h"
    #include "order.h"
    #include <string>
    #include <vector>

    using namespace std;

    class Order;

    class Client {
        string first_name;
        string last_name;
        int account_balance;
        Country country;
        Currency currency;
        vector <Order> orders;

    public:
        Client (string first_name, string last_name, Country country, Currency currency);
        void buy_item (Item item, unsigned quantity, unsigned order_ID);
        void add_order (unsigned ID);
        virtual void pay (unsigned order_ID) = 0; //
    };

    #endif // CLIENT_H

抽象类 - .cpp 文件:

   #include "client.h"

   Client::Client (string first_name, string last_name, Country country, Currency currency)
       : first_name(first_name), last_name(last_name), country(country), currency(currency)
   {
   account_balance = 0;
   }

继承类-头文件:

    #ifndef ENGLISHCLIENT_H
    #define ENGLISHCLIENT_H
    #include "client.h"
    #include <string>

    using namespace std;

    class EnglishClient : public Client {
    public:
        EnglishClient (string first_name, string last_name);
        void pay (unsigned order_ID);
    };

    #endif // ENGLISHCLIENT_H

继承类 - .cpp 文件:

    #include "englishclient.h"

    EnglishClient::EnglishClient (string first_name, string last_name)
        : Client(first_name, last_name, GB, GBP)
    {
    }

最后是错误:

GB 和 GBP 是枚举变量:

enum Country {GB, PL};

enum Currency {GBP, PLN};

【问题讨论】:

  • Client 类中有虚拟 dtor?

标签: c++ inheritance abstract-class


【解决方案1】:

你忘记了方法Client::buy_item、Client::add_order和EnglishClient::pay的实现

【讨论】:

  • 我想我可以在实现这些方法之前编译这段代码。无论如何,它现在可以工作了,非常感谢:)
【解决方案2】:

您从未定义过EnglishClient::pay、Client::buy_item 或Client::add_order。对于客户端,要么定义它们,要么将它们设置为抽象:void buy_item (Item item, unsigned quantity, unsigned order_ID)=0;(与 add_order 相同)`并在子类中定义它们。

【讨论】:

    猜你喜欢
    • 2012-03-13
    • 2015-04-08
    • 2014-09-24
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多