Passing a pointer to an object that is stored in a vector (SOLVED!)

Started by
6 comments, last by darenking 18 years, 9 months ago
Hello! In my game, I have a class called Character, that stores the data for each character in my game, for example the images to use for that character, the width and height, etc. I also have a class called Actor. Instances of this class are the actual creatures in the game. So for example if I was writing a PacMan game, there would be two Characters, the PacMan character and the Ghost character, and there would be five Actors, four of which would hold a pointer to the Ghost character and one would hold a pointer to the PacMan character. Anyway, the point is, I want to send the Character pointers to the Actor objects, so each Actor object can look/behave like the correct Character. I know how to pass pointers to objects to an object, as some people on this site recently showed me how to send a pointer to the Maze object to each Actor, so that each Actor can see the Maze. Trouble is, my Character objects are stored in a vector, and although I know how to access each Character like this: m_Characters[PACMAN]->AnimFirst(WALKLEFT, 9); m_Characters[GHOST]->AnimFirst(WALKLEFT, 20); ...I don't know how to send a pointer to one of these two Characters to the Actors. The Actor object is ready to receive the pointer and store it:

void Actor::CharacterPut(Character* const character)
{
	m_pCharacter = character;
}


I send the Actors their initial X and Y position like this: m_Actors[PLAYER]->PosPut(100,200); I have tried to send them the pointer to their Character like this: m_Actors[PLAYER]->CharacterPut( &m_Characters[0] ); ...but I get an error: In member function `bool World::CreatePopulate(std::ifstream&)': CharacterPut(Character**)' candidates are: void Actor::CharacterPut(Character*) Any ideas? Is there a special way of accessing the address of an object that is stored in a vector? [Edited by - darenking on July 4, 2005 7:48:40 AM]
Advertisement
try just passing

m_Actors[PLAYER]->CharacterPut( m_Characters[0] );

without the ampersand.
Try:
m_Actors[PLAYER]->CharacterPut( ( Character* const )m_Characters[0] );

Hope that helps.
It is not that hard. Judging from these calls:
m_Characters[PACMAN]->AnimFirst(WALKLEFT, 9);m_Characters[GHOST]->AnimFirst(WALKLEFT, 20);

Your character vector is defined along the lines of:
std::vector<CCharacter*> m_Characters;

This means that every entry of the vector is a pointer to a character instance. You have already used the index operator [] to obtain such pointers, only you used them to call member functions:
m_Characters[PACMAN]->AnimFirst(WALKLEFT, 9);

You can use the same to pass the pointer to the actor object:
m_Actors[PLAYER]->CharacterPut( m_Characters[PACMAN] );

Hope this helps you out! Greetz,

Illco
Quote:Original post by Anonymous Poster
try just passing

m_Actors[PLAYER]->CharacterPut( m_Characters[0] );

without the ampersand.


Won't that pass the whole object rather than just the address?

If that does work, why do I need the amperstand when I send a pointer to an object that isn't stored in a vector? I send the address of my Maze object like this (when I create the Actor)...

Actor* newActor = new Actor(&m_Maze);

and it is recieved like this:

Actor::Actor(Maze* const maze){	m_pMaze = maze;//etc


Have I done something wrong there - is the amperstand not needed?

Quote:Original post by Illco
This means that every entry of the vector is a pointer to a character instance. You have already used the index operator [] to obtain such pointers, only you used them to call member functions:

Illco


Of course! The vector already holds pointers to objects rather than objects! That's why I don't need the &. That's right isn't it?
Absolutely. For the future: you could have judged that from the error your compiler gave. It said it could not convert a Character** parameter to the Character* parameter the function required. You can deduce that you had dereferenced the pointer one too many times.
Thanks all!

I think this matter is closed.

This topic is closed to new replies.

Advertisement