CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    15

    Polymorfism problem (in linking)

    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);

    };

  2. #2
    Join Date
    Sep 2002
    Posts
    1,747
    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.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449
    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:
    Code:
    Bird::~Bird()
    {
    }
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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().
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Mar 2003
    Posts
    15

    thank you!

    Thank you for fast and correct answer, missing destructor in .cpp file fixed the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured