https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82526

            Bug ID: 82526
           Summary: Confusing error for constructor of member
           Product: gcc
           Version: 6.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jan.willem.ps at gmail dot com
  Target Milestone: ---

When your class has multiple members of the same type, and in the initializer
list you initialize the last one, but forget to initialize a previous one, you
get a very confusing error message.
The compiler error shows an error on the end of the initializer list, which in
this case points to the member that has been correctly initialized. It doesn't
mention the name of the member that has the error, so it is easy to confuse the
error message with the member it is pointing to..

The example will demonstrate clearly:

// file baderror.cpp
class Point {
 public:
  explicit Point(int x, int y);
};

class Rectangle {
 public:
  Rectangle(int x1, int x2, int y1, int y2)
      :
      mBottomRight(x1, y1)
  {}
 private:
  Point mTopLeft;
  Point mBottomRight;
};

# compiling:
docker run --rm -v $PWD:/src -w /src gcc:6.4.0 gcc baderror.cpp

# output:
baderror.cpp: In constructor 'Rectangle::Rectangle(int, int, int, int)':
baderror.cpp:10:26: error: no matching function for call to 'Point::Point()'
       mBottomRight(x1, y1)
                          ^
baderror.cpp:3:12: note: candidate: Point::Point(int, int)
   explicit Point(int x, int y);
            ^~~~~
baderror.cpp:3:12: note:   candidate expects 2 arguments, 0 provided
baderror.cpp:1:7: note: candidate: constexpr Point::Point(const Point&)
 class Point {
       ^~~~~
baderror.cpp:1:7: note:   candidate expects 1 argument, 0 provided
baderror.cpp:1:7: note: candidate: constexpr Point::Point(Point&&)
baderror.cpp:1:7: note:   candidate expects 1 argument, 0 provided

In this case there is no mention of member mTopLeft, which hasn't been
initialized. So the compiler tries to call the default constructor, which
doesn't exist. The only clue is that there are 0 arguments, but no reference to
the member it is complaining about.

In short: it would be very helpful to have the member name in the error
message.

Reply via email to