Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

(Removed original text as it is unrelated to the current question which has already been answered. See revisions.)

Here is my example test.hpp (simplified):

class House {
    private:
        int nWindows;
    public:
        House(int nWindows);
        int getNumberOfWindows();
};

class PaintedHouse : public virtual House {
    private:
        int colorCode;
    public:
        PaintedHouse(int nWindows, int colorCode);
        int getColorCode();
};

class OccupiedHouse : public virtual House {
    private:
        int nPeople;
    public:
        OccupiedHouse(int nWindows, int nPeople);
        int getNumberOfPeople();
};

class PaintedOccupiedHouse : public PaintedHouse, OccupiedHouse {
    public:
        PaintedOccupiedHouse(int nWindows, int colorCode, int nPeople);
};

And test.cpp:

#include "test.hpp"

House::House(int nWindows) { this->nWindows = nWindows; }
int House::getNumberOfWindows() { return this->nWindows; }

PaintedHouse::PaintedHouse(int nWindows, int colorCode) : House(nWindows) {
    this->colorCode = colorCode;
}
int PaintedHouse::getColorCode() { return this->colorCode; }

OccupiedHouse::OccupiedHouse(int nWindows, int nPeople) : House(nWindows) {
    this->nPeople = nPeople;
}
int OccupiedHouse::getNumberOfPeople() { return this->nPeople; }

PaintedOccupiedHouse::PaintedOccupiedHouse(int nWindows, int colorCode, int nPeople)
            : PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}

GCC returns:

test.cpp: In constructor ‘PaintedOccupiedHouse::PaintedOccupiedHouse(int, int, int)’:
test.cpp:18:72: error: no matching function for call to ‘House::House()’
    : PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}
                                                                        ^
test.cpp:18:72: note: candidates are:
test.cpp:4:1: note: House::House(int)
 House::House(int nWindows) { this->nWindows = nWindows; }
 ^
test.cpp:4:1: note:   candidate expects 1 argument, 0 provided
In file included from test.cpp:2:0:
test.hpp:2:7: note: constexpr House::House(const House&)
 class House {
       ^
test.hpp:2:7: note:   candidate expects 1 argument, 0 provided
test.hpp:2:7: note: constexpr House::House(House&&)
test.hpp:2:7: note:   candidate expects 1 argument, 0 provided

Could you give me any advice what I am doing wrong? Is the general concept correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
119 views
Welcome To Ask or Share your Answers For Others

1 Answer

Replace

PaintedOccupiedHouse::PaintedOccupiedHouse(int nWindows, int colorCode, int nPeople)
            : PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}

by

PaintedOccupiedHouse::PaintedOccupiedHouse(int nWindows, int colorCode, int nPeople)
            : House(nWindows), PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}

When you have virtual inheritance, there is only one instance of the virtual base class. It must be initialized in the constructor of the most derived class being constructed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...