lundi 25 mai 2020

virtual inheritance issue - warning: 'Sofa' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit

I had an issue for virtual inheritance, it's a sample code for virtual inheritance.

I got the issue as below

warning: 'Sofabed' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit

Bed and Sofa also got same warning.

How could I fixed this issue?

Run this sample code on

  1. Mac OS 10.14.6
  2. Qt Creator 4.11.1
  3. Based on Qt 5.14.1 (Clang 10.0 (Apple), 64 bit)
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

class Furniture { //common base class
protected: int weight;
public: 
    void SetWeight(int a=0) { weight=a; }
    int GetWeight() { return weight; }
    void ShowWeight() { 
        cout << "weight=" << weight << endl;
    } 
};

class Sofa : virtual public Furniture {
public: 
    void sit() { cout << "sit!" << endl; }
    //void ShowWeight() { 
    //    cout << “Sofa weight=” << weight; } 
};

class Bed : virtual public Furniture {
public: 
    void lie() { cout << "lie!" << endl; }
    //void ShowWeight() { 
    //    cout << “Bed weight=” << weight; } 
};

class Sofabed : public Sofa, public Bed {
public: 
    void fold() { cout << "fold!" << endl; }
};


int main() 
{
    Sofabed obj;
    obj.sit();
    obj.lie();
    obj.fold();

    obj.SetWeight(100);
    obj.ShowWeight();

    return 0;   
}

Aucun commentaire:

Enregistrer un commentaire