Avatar of Fluffy_Checkers
Fluffy_Checkers
 asked on

CArray's / Polymorphism

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
C++

Avatar of undefined
Last Comment
Priyesh

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Priyesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cup

Don't forget the release version if you are building for both debug and release.
Fluffy_Checkers

ASKER
Privesh....
Excellent, fixed the problem. I don't suppose you could spare a few comments to explain the who, whats and whys of that...? Is it just a project default qwerk or something specific the way I was coding???

Regards Paul
Fluffy_Checkers

ASKER
Thanks a lot fixed the problem. I was just wondering if you get the chance, to explain what was going on, etc? Is it just a default project qwerk??

Thanks Again =)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Priyesh

__beginthreadex and __endthreadex are in the multithreaded version of the runtime library. when you start a console project, you will have to change the project settings if you plan to use multiple threads. In your case, you do not have implicit thread creation from your code, but the MFC libraries you include for CArray use multithreading. Therefore, you will have to change the settings to link with the multithreaded libraries.

you will not notice this if you program in MFC because the default project settings are for multithreaded.

hope that explains it. good luck!