Item 14 in 'Effective C++'

B

Bart Vandewoestyne

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
 
I

Ian Collins

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?
 
B

Bart Vandewoestyne

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top