Sign in to follow this  
Stevene

Pointers in c++

Recommended Posts

Stevene    122
Im trying to store a list of images for an object in a vector using the ZEngine 2d engine. I store the current frame (because of my inheritance method - GME_AObject derived from GME_Object) and i can't find the way to get a reference for the current frame using the iterator. it gives a compiler error, not a run-time: Relevant code: GME_Object.h
...
        public:
            virtual void update();
        protected:
            ZImage* image;

GME_AObject.h
        public:
            virtual void update();
	protected:
	    vector<ZImage> Frames;
	    vector<ZImage>::iterator frame;
	    Uint32 frametime;
	    Uint32 timepassed;

GME_Object.cpp
void GME_Object::update()
{
    x=x+(rEngine->GetFrameTime()*hspeed);
    y=y-(rEngine->GetFrameTime()*vspeed);
}

GME_AObject.cpp
void GME_AObject::update()
{
	this->GME_Object::update();
	timepassed += (int)(rEngine->GetFrameTime()+0.5); //0.5 is to round up if nec
	if (timepassed > frametime)
	{
	   frame++;
	   if (frame != Frames.end())
	   image = *(Frames.at(frame)); <- COMPILER ERROR
           {
             frame = Frames.begin();
             image = *(Frames.at(frame)); // <- COMPILER ERROR
           }
	   timepassed = 0;
	}
}


any help would be much appreciated! oh yeah, also tried image = &(Frames.at(frame)); [edit: added source tags -SiCrane]

Share this post


Link to post
Share on other sites
Enigma    1410
frame is an iterator. Since an iterator is "like" a pointer and refers to an element of a container you do not need to index into the container with it. To do would be like asking for "the element at the element at position n", which makes no sense. If you have an iterator and which to access the element the iterator refers to simply dereference the iterator: *frame (or use the pointer to member operator).

Enigma

Share this post


Link to post
Share on other sites
Enigma    1410
Dereferencing an iterator yields a reference to the element. To get a pointer to the element you will have to take it's address, just like normal, i.e. &*frame. There doesn't really seem to be much point in storing a pointer when you already have an iterator though.

Enigma

Share this post


Link to post
Share on other sites
Stevene    122
Hmm, tried every combination of & and *'s under the sun, and nothing, lol. the only reason for needing it is the bad underlying structure of my code.
GME_Object is designed for still images, and creates a pointer to that image at creation, GME_AObject is designed for animated objects, and inherits that pointer from GME_Object. and to save overiding the ::Draw() function, i thought it would be easier to just copy the address of the current frame to the pointer holder.
Thanks so much for the help, but i give up, going to go the less lazy (or not, on reflection) route. lol. thanks a lot for the time though

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this