I am not sure why this is not working.

Started by
5 comments, last by BitMaster 10 years, 9 months ago

Hello, I have been pondering an error I have had for a few days now and I just cannot seem to figure out the problem. I have an abstract base class called Entity which my PlayerEntity class is inheriting from. Entity has a function called setPosition and takes no parameters I am trying to use the setPosition function in my PlayerEntity's constructor however when I do I get a window that says Debug Assertion Failed! and then says vector subscript is out of range. I have narrowed down the problem to my setPosition function. Thank you for any help.

Entity::setPosition:


void Entity::setPositon(){
	
	//set the choords of the vertecies on the vertex array
	m_sfEntityBox[0].position = sf::Vector2f(m_sfPosition.x, m_sfPosition.y);
	m_sfEntityBox[1].position = sf::Vector2f(m_sfPosition.x + m_sfSize.x, m_sfPosition.y);
	m_sfEntityBox[2].position = sf::Vector2f(m_sfPosition.x + m_sfSize.x, m_sfPosition.y + m_sfSize.y);
	m_sfEntityBox[3].position = sf::Vector2f(m_sfPosition.x, m_sfPosition.y + m_sfSize.y);
}
Advertisement

Are you sure that m_sfEntityBox has four elements in it? If not, you should create them first by using push_back or resize on the vector.

But it is not a vector. sf_EntityBox is a sf::VertexArray of sf::Quads. Sorry, I probably should have said that.

Internally, SFML's VertexArray uses a vector for storage. (Well at least some versions of SFML anyways.) And like a vector, it needs to be sized properly before you use operator[] on it.

Well, in this case have you ensured that m_sfEntityBox.getVertexCount() returns at least four and called resize if that is not the case? On a side note, according to code available online, sf::VertexArray is just an extremely thin wrapper around an std::vector.

Well, in this case have you ensured that m_sfEntityBox.getVertexCount() returns at least four and called resize if that is not the case?

Yes that was the problem thanks. I set the sf::VectorArray in the constructor of the Entity, and the way I was doing it before worked, which was sloppy and looked bad so I changed it. Thanks.

Edit: For the function void Entity::setPosition() do I need to use "this" for all the variables? I would think so because I am not using the function for the Entity but for a child class. It works as is, I am just wondering.

I would think so because I am not using the function for the Entity but for a child class. It works as is, I am just wondering.


You do not need to, neither is it common to write it. I would strongly suggest getting on a more firm footing with C++ before adding the additional complication of external libraries like SFML.

This topic is closed to new replies.

Advertisement