Jump to content
  • Advertisement
Sign in to follow this  

Vector iterator not derefranceable

This topic is 2025 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

Hello, I am working on an animation class that has one std::vectors of textures for both a left and right direction of textures. The point of this class it to hold the different frames and then after a specific amount of time has passed, update the frame and then return a texture for rendering. When I compile the code I get this window(attacked file). Here is the animation class:




#include <SFML/Graphics.hpp>
#include <vector>

enum DIRECTION{LEFT = 1, RIGHT = 0};

class Animation{

	void setAnimation(sf::Image sheet, sf::Vector2f frameSize);
	void setDirection(DIRECTION dir);
	void update(sf::Time deltaT);

	sf::Texture& getCurrentFrame(){return *m_Iter;}

	sf::Image m_sfSpriteSheet;
	sf::Vector2f m_sfFrameSize;
	std::vector<sf::Texture> m_LeftFrames;
	std::vector<sf::Texture> m_RightFrames;
	std::vector<sf::Texture>::iterator m_Iter;
	int m_iXFrames , m_iYFrames, m_iCurrentFrame;
	DIRECTION m_CurrentDirection;
	sf::Time m_sfFrameTime;




#include "Animation.hpp"

Animation::Animation() : m_CurrentDirection(LEFT), m_sfFrameTime(sf::seconds(0)){}


void Animation::setAnimation(sf::Image sheet, sf::Vector2f frameSize){

	m_sfSpriteSheet = sheet;
	m_sfFrameSize = frameSize;

	//get the size of the sheet so I can set the vector of texures
	m_iYFrames = (int)(m_sfSpriteSheet.getSize().y / m_sfFrameSize.y);
	m_iXFrames = (int)(m_sfSpriteSheet.getSize().x / m_sfFrameSize.x);

	sf::Texture tempTex;

	//split up sheet
	for(int y = 0; y < m_iYFrames; ++y){

		for(int x = 0; x < m_iXFrames; ++x){

			tempTex.loadFromImage(m_sfSpriteSheet, sf::IntRect((int)(x * m_sfFrameSize.x), (int)(y * m_sfFrameSize.y), 
				                  (int)(m_sfFrameSize.x), (int)(m_sfFrameSize.y)));

			if(y == 0) m_LeftFrames.push_back(tempTex);
			if(y == 1) m_RightFrames.push_back(tempTex);

	*m_Iter = m_LeftFrames[0];

void Animation::setDirection(DIRECTION dir){

	m_CurrentDirection = dir;
	m_iCurrentFrame = 0;

	if(m_CurrentDirection == LEFT) *m_Iter = m_LeftFrames[m_iCurrentFrame];
	if(m_CurrentDirection == RIGHT) *m_Iter = m_RightFrames[m_iCurrentFrame];


void Animation::update(sf::Time deltaT){

	m_sfFrameTime += deltaT;

	//If one second as passed then change frame
	if(m_sfFrameTime >= sf::seconds(1)){

		m_sfFrameTime = sf::seconds(0);
		if(m_iCurrentFrame == m_iXFrames) m_iCurrentFrame = 0;

		if(m_CurrentDirection == LEFT) *m_Iter = m_LeftFrames[m_iCurrentFrame];
		if(m_CurrentDirection == RIGHT) *m_Iter = m_RightFrames[m_iCurrentFrame];


Edited by Crusable

Share this post

Link to post
Share on other sites

You have an iterator. The iterator never is assigned anything (as SiCrane noticed). However, the iterator is dereferenced so what it is (not) pointing at can be assigned to.



int *myInt = NULL; //Doesn't point to anything.
*myInt = 5; //Crash!


For example, you have:


*m_Iter = m_LeftFrames[0];


This says, "take what m_Iter" is already pointing to, and assign the value of m_LeftFrames[0] to it". It's not saying, "make it point to m_LeftFrames[0]", which I think is what you are wanting. Since m_Iter isn't yet pointing to anything, it's similar (in concept) to dereferencing a null pointer.


A normal pointer will work just as fine, but if you want a iterator, you have to do it like this:


m_Iter = m_LeftFrames.begin(); //Gets an iterator to the first element.
m_Iter = (m_LeftFrames.begin() + index); //Gets an iterator to the nth element.


Note that we assigned to the iterator without dereferencing it. Dereferencing gets the object it is pointing to.

Warning: Your iterators will become invalid if the container is resized or elements inserted or erased.

Edited by Servant of the Lord

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!