Click to See Complete Forum and Search --> : Polymorfism problem (in linking)


t0by
April 10th, 2003, 01:23 PM
Hello all. I would be extremely glad if someone could guide me to the right path:

I learning c++ polymorfism mechanisms, and these
errors give me trouble:

bird.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Bird::~Bird(void)" (??1Bird@@UAE@XZ)
owl.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Bird::~Bird(void)" (??1Bird@@UAE@XZ)
parrot.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Bird::~Bird(void)" (??1Bird@@UAE@XZ)

Classes that I have (and I use inheritance in them):
Animal
Bird
Owl
Parrot

Is there a mismatch between the functions I implement and the inheritance mechanism (with virtual functions or something)?

I list the functions I implement in .cpp files below, and under them the .h files.

//animal.cpp
Animal::Animal(char *name, float mass, float energy)
Animal::~Animal()
void Animal::eat(float food)
char* Animal::getName()
float Animal::move(float distance)

//bird.cpp
Bird::Bird(char *name, float mass, float energy, float wingspan) : Animal(name,mass,energy)
void Bird::lay()
float Bird::move(float distance)
void Bird::say(const char *sentence)

//parrot.cpp
Parrot::Parrot(char *name, float mass, float energy, float wingspan) : Bird(name,mass,energy,wingspan)
void Parrot::say(const char *sentence)
Parrot::~Parrot()

//owl.cpp
Owl::Owl(char *name, float mass, float energy, float wingspan) : Bird(name,mass,energy,wingspan)
void Owl::say(const char *sentence)

I list my definitions below:

//Animal.h

class Animal {
Animal(char *name=NULL, float mass=1.0, float energy=0.0);
virtual ~Animal();
void eat(float food);
char *getName();

protected:
char *name; /**<- name of the animal */
float mass; /**<- mass in kilograms */
float energy; /**<- energy in joules */

};


//bird.h

class Bird : public Animal {

public:

Bird(char *name=NULL, float mass=1.0, float energy=0.0, float wingspan=1.0);

virtual void say(const char *sentence);
virtual void lay();
virtual ~Bird();
virtual float move(float distance);

protected:
float wingspan; /**<- wingspan of the bird */

};

//parrot.h
class Parrot : public Bird {

public:

Parrot(char *name=NULL, float mass=1.0, float energy=0.0, float wingspan=1.0);

virtual ~Parrot();
virtual void say(const char *sentence);

char *previousSentence;
};



//owl.h

class Owl : public Bird {

public:

Owl(char *name=NULL, float mass=1.0, float energy=0.0, float wingspan=1.0);

virtual void say(const char *sentence);

};

galathaea
April 10th, 2003, 01:38 PM
It looks like you tell the compiler in your header that there is a destructor for Bird, but your list doesn't show you have it in your implementation file. If you tell your compiler you are making ~Bird(), put an actual definition for it in your .cpp.

Paul McKenzie
April 10th, 2003, 01:40 PM
The compiler cannot find the Bird destructor implementation.

It seems you implemented the destructor for your derived classes, but where is the destructor for the base class implemented (Bird in this case)?

If the Bird destructor does nothing, then the implementation is just an empty code block:

Bird::~Bird()
{
}

Regards,

Paul McKenzie

Graham
April 10th, 2003, 01:46 PM
I concur with galathea and Paul.

bird.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Bird::~Bird(void)" (??1Bird@@UAE@XZ)

.... and that's exactly what the linker error is telling you. "unresolved external symbol" basically means "I can't find..." and then it tells you what it can't find, namely Bird::~Bird().

t0by
April 10th, 2003, 01:49 PM
Thank you for fast and correct answer, missing destructor in .cpp file fixed the problem.