Greetings,
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.
<START CODE>
#include "Afxtempl.h"
#include <stdio.h>
class animal
{
protected:
int legs;
public:
void NumLegs(void) { printf("I have %d legs\n", legs); }
virtual void speak(void) { };
};
class dog : public animal
{
public:
dog() { legs = 4; }
void speak(void) { printf("Woof\n"); }
};
class cat : public animal
{
public:
cat() { legs = 4; }
void speak(void) { printf("Meow\n"); }
};
class bird : public animal
{
public:
bird() { legs = 2; }
void speak(void) { printf("Chirp\n"); }
};
class zoo
{
public:
CArray<animal *, animal *> animals;
void AddAnimal(animal *a);
void FreeAnimals(void);
void ListAnimals(void);
};
void zoo::AddAnimal(animal *a)
{
animals.Add(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);
a->NumLegs();
a->speak();
}
}
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;
z->AddAnimal(a);
z->AddAnimal(b);
z->AddAnimal(c);
z->AddAnimal(d);
z->AddAnimal(e);
z->ListAnimals();
z->FreeAnimals();
delete(z);
return 0;
}
<END CODE>
These are the compilation errors...
--------------------Configuration: polymorphism - Win32 Debug--------------------
Compiling...
polymorphism2.cpp
Linking...
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