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

            Bug ID: 93716
           Summary: [feature request] Improve error message for Classes
                    without a default constructor
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: volker.weissmann at gmx dot de
  Target Milestone: ---

Consider the following C++ source code:
-------------------------------------
class NoDefaultConstr
{
public:
    NoDefaultConstr(int i)
    {
        (void) i;
    }
};
class BadClass
{
public:
    NoDefaultConstr obj;
    int othervar;
    BadClass(int i):
        othervar(17)
    {
        (void) i;
    }
};
int main()
{
    BadClass abc(123);
}
-------------------------------------
Compilation of the code above should fail. Compiling with g++ main.cpp fails
with the following output:
-------------------------------------
main.cpp: In constructor ‘BadClass::BadClass(int)’:
main.cpp:15:20: error: no matching function for call to
‘NoDefaultConstr::NoDefaultConstr()’
   15 |         othervar(17)
      |                    ^
main.cpp:4:5: note: candidate: ‘NoDefaultConstr::NoDefaultConstr(int)’
    4 |     NoDefaultConstr(int i)
      |     ^~~~~~~~~~~~~~~
main.cpp:4:5: note:   candidate expects 1 argument, 0 provided
main.cpp:1:7: note: candidate: ‘constexpr
NoDefaultConstr::NoDefaultConstr(const NoDefaultConstr&)’
    1 | class NoDefaultConstr
      |       ^~~~~~~~~~~~~~~
main.cpp:1:7: note:   candidate expects 1 argument, 0 provided
main.cpp:1:7: note: candidate: ‘constexpr
NoDefaultConstr::NoDefaultConstr(NoDefaultConstr&&)’
main.cpp:1:7: note:   candidate expects 1 argument, 0 provided
-------------------------------------
Compiling fails as it should, but it would be nice if the error message would
be a bit more helpful. e.g.:
-------------------------------------
main.cpp: In constructor ‘BadClass::BadClass(int)’:
main.cpp:15:20: error: no matching function for call to
‘NoDefaultConstr::NoDefaultConstr()’
   15 |         othervar(17)
      |                    ^
main.cpp:12:23: note: Because of declaration of object ‘obj’
   12 |     NoDefaultConstr obj;
      |
main.cpp:4:5: note: candidate: ‘NoDefaultConstr::NoDefaultConstr(int)’
    4 |     NoDefaultConstr(int i)
      |     ^~~~~~~~~~~~~~~
main.cpp:4:5: note:   candidate expects 1 argument, 0 provided
main.cpp:1:7: note: candidate: ‘constexpr
NoDefaultConstr::NoDefaultConstr(const NoDefaultConstr&)’
    1 | class NoDefaultConstr
      |       ^~~~~~~~~~~~~~~
main.cpp:1:7: note:   candidate expects 1 argument, 0 provided
main.cpp:1:7: note: candidate: ‘constexpr
NoDefaultConstr::NoDefaultConstr(NoDefaultConstr&&)’
main.cpp:1:7: note:   candidate expects 1 argument, 0 provided
-------------------------------------
The reason why I do not like gcc's error message is that it never mentions the
line 12 or the variable obj. Clang in comparison mentions both and has imho a
much nicer error message:
---------------------
main.cpp:14:5: error: constructor for 'BadClass' must explicitly initialize the
member 'obj' which does not have a default constructor
    BadClass(int i):
    ^
main.cpp:12:21: note: member is declared here
    NoDefaultConstr obj;
                    ^
main.cpp:1:7: note: 'NoDefaultConstr' declared here
class NoDefaultConstr
      ^
---------------------

Reply via email to