Coding error causes odd error message
Martin von Loewis
martin@mira.isdn.cs.tu-berlin.de
Thu May 21 19:12:00 GMT 1998
> The following code snippet causes the compiler to not properly
> generate the constuctor base::base(). This leads to a highly
> misleading message at link time. The problem here is that
> the DTOR is not defined, not the ctor. Removing the keyword
> "virtual" from the dtor makes the compiler generate the ctor
> properly.
Andrew,
Thanks for your bug report. Unfortunately, this is hard to fix, and
really not a bug in g++.
First, your program is wrong. All virtual functions of a class must be
defined in a program where instances of that class are created. So the
compiler is right to complain.
As for the message being misleading: I'll try to explain it, not to
excuse it. g++ puts inline functions and the virtual method into a
dedicated object file: the one that contains the first virtual
function of the class. Since there is such a function, there must be
a file that implements it; it is a your fault that you didn't provide
that implementation.
So, since base::~base is not implemented, the out-of-line
implementation of base::base() is not created, and missing when
linking. Normally, you would also get a missing reference to the
virtual table. In your case, you get that only when compiling with
-O.
Also, since the destructor is virtual, it is only referenced from the
virtual table. Since the virtual table is not emitted by the compiler,
the destructor is not missing when linking.
There might be some linker tricks to point to the actual cause. As I
said, this is difficult.
Martin
More information about the Gcc-bugs
mailing list