• Advertisement
Sign in to follow this  

Inherited Classes in Class Array?

This topic is 4359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my main() so far-
#include "StdAfx.h"

int main()
{	
	//Creating Objects
	Actor *pActorArray[2];
	Player *pPlayer = new Player();
	Enemy *pEnemy = new Enemy();
	
	//Setting up Array...
	pActorArray[0] = pPlayer;
	pActorArray[1] = pEnemy;

	cout << pActorArray[0]->getAnimationName(2) << endl;
	cout << pActorArray[1]->getAnimationName(4) << endl;


	 for( int Seek = 0; Seek < 2; Seek++ )
	{	
		//This works fine
		pActorArray[Seek]->getAnimationName(2);

		//This doesn't----------------
		pActorArray[Seek]->Modify(5);
	}

	getch();
	return 0;
}

the error I'm receiving is- main.cpp(24) : error C2039: 'Modify' : is not a member of 'Actor' Which I know, its a member of Player and Enemy (It does different things in both classes though) Is what I'm trying to do even possible? Or would I have to create 2 separate arrays, one to store Players and the other to store Enemy's (even though they are both derived from Class 'Actor'?) Please, tell me if I need to elaborate :)

Share this post


Link to post
Share on other sites
Advertisement
if I understand you, Player is derived from Actor.. player has a member 'Modify'

Actor *a is assigned to players memory
you want a->Modify() to access players Modify..
just put a 'virtual' in front of Actors 'Modify' declaration

[edit]
(just to explain a bit)

if you declare a virtual function in a base class, then reference that
function using a base class pointer to a derived class the base class will
'lookup' the function in the derived class, and if present, will execute it..
if its not present, the base class virtual function will be called..
to assure a derived class provides this function you can put
'virtual derived_must_provide() = 0;' in the base class.. This just tells the
compiler that all derived classes must provide a 'derived_must_provide()'
member.. also if you do the '= 0' thing, you cannot use an instance of the
base class but must use an instance of the derived class (this will result in
a compiler error if you try to 'base something;'

Share this post


Link to post
Share on other sites
Sounds like, while Modify is in both Player and Enemy, it's not in Actor (the common base class). You'll want to make sure Actor has a Modify as well, and make it virtual. Failing that, post the class definitions.

(I should note, however, that making it virtual is not required for the code to compile; however without it you won't get the runtime behavior you probably want).

Share this post


Link to post
Share on other sites
You said you have the Modify function in both Player and Enemy but you didn't mention it being in the Actor base class. If it's not in the base class, what you're doing won't work. The fact that they have the same function and inherit the same base is not enough. You should have something like this in your base.

class Actor
{
public:
virtual void Modify(int);
};




The virtual keyword tells it to use a derived class's version of the function.
That should let you do what you're trying to do.

Hope that helps.

[Edit] I was second to respond when I started this ... you people type like mad-men.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement