• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# I am not sure why this is not working.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Crusable  Members

Posted 05 July 2013 - 12:45 AM

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);
}


### #2rnlf  Members

Posted 05 July 2013 - 01:02 AM

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.

### #3Crusable  Members

Posted 05 July 2013 - 04:45 AM

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

### #4SiCrane  Moderators

Posted 05 July 2013 - 04:50 AM

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.

### #5BitMaster  Members

Posted 05 July 2013 - 04:52 AM

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.

### #6Crusable  Members

Posted 05 July 2013 - 06:41 AM

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.

Edited by Crusable, 05 July 2013 - 06:43 AM.

### #7BitMaster  Members

Posted 05 July 2013 - 07:13 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.