bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Misleading link error message for undefined virtual method


From: Geoff Alexander
Subject: Misleading link error message for undefined virtual method
Date: Thu, 29 Mar 2001 15:03:46 -0500

I just spent a half a day debugging a problem that turn out to be a
misleading link error message.  Here's a small test that illustrates the
problem:

     #include <iostream>

     class Base {
       public:
         inline Base(int i) : value(i) {}
         inline int getValue() { return value; }
         virtual void f1();
         virtual void f2();
         int value;
     };

     void Base::f1() { cout << "Base::f1()" << endl; }
     void Base::f2() { cout << "Base::f2()" << endl; }

     class Derived : public Base {
       public:
         inline Derived (int i) : Base(i) {}
         inline virtual void Derived::f1() { cout <<
     "Derived::f1()" << endl; }
         virtual void Derived::f2();
     };

     //void Derived::f2() { cout << "Derived::f2()" << endl; }

     int main(int argc, char* argv) {
       Base* b = new Derived( 100 );
       cout << "This is a test: " << b->getValue() << endl;
       delete b;
     }

Note that the definition of Derived::f2() is commented out.  Here's the
output of compiling and linking:

      g++ -g -o Test Test.cpp
     /tmp/cc2PdC4I.o: In function `main':
     /home/gdlxn/g++-bugs/001/Test.cpp:25: undefined reference to
     `Derived::Derived(int)'
     collect2: ld returned 1 exit status

I would expect this to compile and link successfully since there are no
calls to either Base::f2() or Derived::f2().  At least, the error
message should indicate that Derived::f2(), rather than
`Derived::Derived(int), is undefined.  Note that the code compiles and
links fine if I either uncomment the definition of Derived::f2() or if I
remove the virtual keyword from Base::f2() and Derived::f2().  I'm
running g++ 2.95.2 on Suse Linux for i386 7.0.  Here's the output of g++
-v:

     Reading specs from
     /usr/lib/gcc-lib/i486-suse-linux/2.95.2/specs
     gcc version 2.95.2 19991024 (release)

Please let me know if any additional information is required.

Thanks.,
Geoff Alexander, Ph.D.
919/254-5216 T/L 444-5216
CMVC95 WebDAV Development
IBM Corporation
RTP, NC





reply via email to

[Prev in Thread] Current Thread [Next in Thread]