Why doesn't my compiler recognise Bond() = default;

  • A+
Category:Languages

Please look at this code

class Bond { public:     Bond(int payments_per_year, int period_lengths_in_months);         Bond() = default;  private:     const int payments_per_year;     const int period_length_in_months; };  int main() {     Bond b; // Error here } 

When attempting to compile I get an error "error C2280: 'Bond::Bond(void)': attempting to reference a deleted function".

It's not a "rule of 3" violation since I've added the default constructor back.

Why doesn't the compiler recognise Bond() = default;?

 


The default constructor is suppressed since there are constant members that need to be explicitly initialised.

Therefore, due to that suppression, writing Bond() = default does not reintroduce the default constructor.

(You can see this effect by removing all the constructors in the class - you still can't instantiate a b.)

If you drop the const from the members then all will be well.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: