troubleshooting Question

CArray's / Polymorphism

Avatar of Fluffy_Checkers
Fluffy_Checkers asked on
5 Comments1 Solution367 ViewsLast Modified:
    I'm having trouble with implementing a CArray for the purpose of polymorphism. I've written the little program to illustrate my problem (I'm using VC 6.0).
    Basically I have a base class (in this case animal) and I have lots of other classes that want to inherit those properties (dog, cat, bird). I then want to talk about a group of the derived classes (zoo) which uses polymorphism. My issue is that VC won't compile it and for the life of me I can't figure out why.
#include "Afxtempl.h"
#include <stdio.h>

class animal
     int legs;
     void NumLegs(void) { printf("I have %d legs\n", legs); }
     virtual void speak(void) { };

class dog : public animal
     dog() { legs = 4; }
     void speak(void) { printf("Woof\n"); }

class cat : public animal
     cat() { legs = 4; }
     void speak(void) { printf("Meow\n"); }

class bird : public animal
     bird() { legs = 2; }
     void speak(void) { printf("Chirp\n"); }

class zoo
     CArray<animal *, animal *> animals;
     void AddAnimal(animal *a);
     void FreeAnimals(void);
     void ListAnimals(void);

void zoo::AddAnimal(animal *a)

void zoo::FreeAnimals(void)
     animal *a;

     for(int c; c < animals.GetSize(); c++) {
          a = animals.ElementAt(c);

void zoo::ListAnimals(void)
     animal *a;

     for(int c; c < animals.GetSize(); c++) {
          a = animals.ElementAt(c);


int main(int argc, char* argv[])
     zoo *z = new zoo;    

     animal *a = new dog;
     animal *b = new cat;
     animal *c = new dog;
     animal *d = new bird;
     animal *e = new dog;




     return 0;
These are the compilation errors...

--------------------Configuration: polymorphism - Win32 Debug--------------------
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/polymorphism.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

polymorphism.exe - 3 error(s), 0 warning(s)

The issues go away is you comment out the CArray definition in zoo (and the references to animals from the memeber functions), but what is wrong with the definition?? All I want is an array of animals (zoo, referred to by pointers) so that I can add more 'animals' and not have to worry too much about what they are.

Kind Regards

Fluffy Checkers

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros