0

I need to set the 3 values in the House class which is derived from Dwelling class but it gives me the error that cannot call protected constructor

class Dwelling {
    int bedrooms;
protected:
    Dwelling(){bedrooms=0;};
    Dwelling(int _b){bedrooms=_b;};
};

class House : public Dwelling {
    int storeys;
    bool garden;
public:
    House() {storeys=0; garden=0;};
    //constructor to set storeys, garden and bedrooms.
        House(int st, bool val, int room){
        if (st >= 1 || st <= 4) {
            storeys = st;
            garden = val;
            Dwelling(room); // Gives me ERROR here
        }
    }
};

int main(){
    House a(2, true, 3);
}
1

1 Answer 1

1

The error has nothing to do with calling the protected constructor, because this line:

Dwelling(room); 

is not a call to the base class constructor at all. It's just a declaration of a variable named room of type Dwelling with a pair of redundant parentheses around the declarator. (c++ syntax can be strange sometimes, and you need to read the error messages closely to figure out what's going on).

To actually call the base class constructor, use a member-initializer-list like this:

House(int st, bool val, int room) : Dwelling(room) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.