Error Notes
C++
02-Wreorder-warning

C++ Error Notes

-Wsign-compare warning error

Issue

Member initialization order in the constructor does not match the order of member declaration in the class.

Details

This warning is triggered when the initialization order of class members in the constructor's initializer list is different from the order in which they were declared within the class. The C++ standard specifies that members are initialized in the order they are declared in the class, not the order specified in the initializer list. This can lead to unexpected behaviors, especially when one member's initialization depends on another.

 
class MyClass {
  int x;
  int y;
public:
  MyClass() : y(0), x(y + 1) {} 
  // Warning here because x is declared before y in the class
};

Solution

To resolve this warning, simply adjust the order of member initializations in the constructor's initializer list to match the declaration order in the class.