Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

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
0
Fluffy_Checkers
Asked:
Fluffy_Checkers
  • 2
  • 2
1 Solution
 
PriyeshCommented:
go to project settings, and in the C++ tab, go to code generation, select debug multithreaded.

0
 
cupCommented:
Don't forget the release version if you are building for both debug and release.
0
 
Fluffy_CheckersAuthor Commented:
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
0
 
Fluffy_CheckersAuthor Commented:
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 =)
0
 
PriyeshCommented:
__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!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now