?
Solved

CArray's / Polymorphism

Posted on 2003-02-21
5
Medium Priority
?
345 Views
Last Modified: 2008-02-01
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
Comment
Question by:Fluffy_Checkers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Priyesh earned 300 total points
ID: 7997498
go to project settings, and in the C++ tab, go to code generation, select debug multithreaded.

0
 
LVL 11

Expert Comment

by:cup
ID: 7997750
Don't forget the release version if you are building for both debug and release.
0
 

Author Comment

by:Fluffy_Checkers
ID: 7997973
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
 

Author Comment

by:Fluffy_Checkers
ID: 7997986
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
 
LVL 9

Expert Comment

by:Priyesh
ID: 7998018
__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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question