Reputation: 25
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);
}
Upvotes: 0
Views: 45
Reputation: 60218
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) {
Upvotes: 1