Dawid P Dawid P - 2 months ago 18
C++ Question

Virtual destructor in simple program causes compiler error: "unresolved external symbol"

I have problem with my simple data base program in C++ (with inheritance and virtual func.)
I've done class hirarchy which represents the object Weapon:

#ifndef Ammu_h
#define Ammu_h

#include <string>
#include <iostream>
using namespace std;

//////////HEADER FILE//////////////
class Weapon{
string name;
char damage;

virtual void show() = 0;
//virtual ~Weapon();

class WhiteArm: public Weapon{
double sharpness;
double defence;


class Axe: public WhiteArm{
Axe(string str, char dmg, double shrp, double def){
name = str;
damage = dmg;
sharpness = shrp;
defence = def;
void show(){
cout << this->name << this->damage << this->sharpness << this->defence << endl;

//class Sword: public WhiteArm{...};
//class Club: public WhiteArm{...};



First of all im not quite sure if my implementation is proper.

  1. My main problem is that when I add a virtual destructor, I get error:
    LNK2001: unresolved external symbol "public: __thiscall Weapon::~Weapon(void)"

    I thought it is necessary to make the destructor virtual when the base class contains virtual methods.

  2. Is it good to make constructors at the end of hierarchy? (like me, upper)

I will appreciate every suggestion to my code
Thanks in advance

dlf dlf

Your virtual destructor still needs to have an implementation, even if you mean for it to be pure virtual. I usually write them using this odd-looking syntax: virtual ~Weapon() = 0 {}.

But that evidently doesn't work with some compilers (all compilers other than Microsoft's?), and rightly so (C++11 draft § 10.4/2):

[ Note: A function declaration cannot provide both a pure-specifier and a definition —end note ] [ Example:

 struct C
    virtual void f() = 0 { }; // ill-formed

Instead, you can either leave out the = 0 or locate the body outside the class definition.