Item 14 in 'Effective C++'

Discussion in 'C++' started by Bart Vandewoestyne, Sep 13, 2012.

  1. I'm trying to run one of the examples from item 14 in Scott Meyer's Effective C++ book. As suggested in the book, I already made the EnemyTarget destructor virtual, so I assume the code should compile and run just fine. This is what I have:

    https://github.com/BartVandewoestyne/cpp/tree/master/books/Effective_C /item14

    I'm compiling with g++ 4.6.2 and get:

    $ g++ main.cpp
    /tmp/ccOt7F5P.o: In function `EnemyTarget::EnemyTarget()':
    main.cpp:(.gnu.linkonce.t._ZN11EnemyTargetC2Ev+0xf): undefined reference to `vtable for EnemyTarget'
    /tmp/ccOt7F5P.o: In function `EnemyTank::EnemyTank()':
    main.cpp:(.gnu.linkonce.t._ZN9EnemyTankC1Ev+0x1f): undefined reference to `vtable for EnemyTank'
    main.cpp:(.gnu.linkonce.t._ZN9EnemyTankC1Ev+0x26): undefined reference to `EnemyTank::numTanks'
    main.cpp:(.gnu.linkonce.t._ZN9EnemyTankC1Ev+0x31): undefined reference to `EnemyTank::numTanks'
    collect2: ld returned 1 exit status

    but I don't see why... I already checked the errate on Scott's website, but can't find anything that helps me forward.

    What am I missing here?

    Regards,
    Bart
     
    Bart Vandewoestyne, Sep 13, 2012
    #1
    1. Advertising

  2. Bart Vandewoestyne

    Ian Collins Guest

    On 09/13/12 10:11 PM, Bart Vandewoestyne wrote:
    > I'm trying to run one of the examples from item 14 in Scott Meyer's Effective C++ book. As suggested in the book, I already made the EnemyTarget destructor virtual, so I assume the code should compile and run just fine. This is what I have:
    >
    > https://github.com/BartVandewoestyne/cpp/tree/master/books/Effective_C /item14
    >
    > I'm compiling with g++ 4.6.2 and get:
    >
    > $ g++ main.cpp
    > /tmp/ccOt7F5P.o: In function `EnemyTarget::EnemyTarget()':
    > main.cpp:(.gnu.linkonce.t._ZN11EnemyTargetC2Ev+0xf): undefined reference to `vtable for EnemyTarget'
    > /tmp/ccOt7F5P.o: In function `EnemyTank::EnemyTank()':
    > main.cpp:(.gnu.linkonce.t._ZN9EnemyTankC1Ev+0x1f): undefined reference to `vtable for EnemyTank'
    > main.cpp:(.gnu.linkonce.t._ZN9EnemyTankC1Ev+0x26): undefined reference to `EnemyTank::numTanks'
    > main.cpp:(.gnu.linkonce.t._ZN9EnemyTankC1Ev+0x31): undefined reference to `EnemyTank::numTanks'
    > collect2: ld returned 1 exit status
    >
    > but I don't see why... I already checked the errate on Scott's website, but can't find anything that helps me forward.
    >
    > What am I missing here?


    You don't define EnemyTank::numTanks in the programme.

    Do you intend to use the destroy() method?

    --
    Ian Collins
     
    Ian Collins, Sep 13, 2012
    #2
    1. Advertising

  3. Even before reading your answer, i figured out my 'mistakes' myself... I just love it when I find things myself! ;-)

    1) You're right about EnemyTank::numTanks.

    2) I also discovered that the destroy() method is virtual in both EnemyTarget and EnemyTank. The book doesn't show an implementation of it. Apparently, the fact that it's virtual in both EnemyTarget and EnemyTank is causingthe linker error. Commenting the function out in both classes makes stuffto compile.

    I guess I should have been more critical about the code-example before simply brain-dead trying to compile it ;-)

    Thanks!
     
    Bart Vandewoestyne, Sep 13, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don Kim
    Replies:
    9
    Views:
    574
    Gregg
    May 23, 2004
  2. Matthias
    Replies:
    25
    Views:
    1,017
    Thorsten Ottosen
    Feb 1, 2005
  3. John
    Replies:
    4
    Views:
    674
    Ioannis Vranos
    Apr 27, 2005
  4. Replies:
    1
    Views:
    443
    benben
    Aug 7, 2005
  5. Replies:
    4
    Views:
    449
Loading...

Share This Page