Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Font [Vector error] Vector erase iterator outside range  (Read 3359 times)

0 Members and 1 Guest are viewing this topic.

Bellato

  • Newbie
  • *
  • Posts: 4
    • View Profile
SFML Font [Vector error] Vector erase iterator outside range
« on: November 23, 2015, 01:53:08 pm »
Hi there,

I use SFML 2.2. MS Visual Studio Express 2013. I'm trying to create a menu for my game.

The problem is when I try to create a text in another class, I'm getting an error that says "Vector erase iterator outside range" and game doesnt want to start... If I use text in the main.cpp everything is fine...

menu.h
#pragma once

#include <SFML/Graphics.hpp>

#define MAX_NUMBER_OF_ITEMS 2

using namespace sf;

class menu
{
public:
    menu(float width, float height);
    ~menu();

    void draw(RenderWindow &window);
    void moveUp();
    void moveDown();

private:
    int selectedItemIndex;
    Font font;
    Text startMenu[MAX_NUMBER_OF_ITEMS];
};
 

menu.cpp
#include <iostream>
#include "menu.h"

menu::menu(float width, float height)
{
        if (!font.loadFromFile("res/consolab.ttf"))
        {
                std::cout << "cannot load font: consolab.ttf" << std::endl;
        }

        startMenu[0].setFont(font);
        startMenu[0].setColor(Color::Red);
        startMenu[0].setString("Play");
        startMenu[0].setPosition(Vector2f(width / 2, (MAX_NUMBER_OF_ITEMS + 1) * 1));

        startMenu[1].setFont(font);
        startMenu[1].setColor(Color::White);
        startMenu[1].setString("Exit");
        startMenu[1].setPosition(Vector2f(width / 2, (MAX_NUMBER_OF_ITEMS + 1) * 2));
}


menu::~menu()
{
}

void menu::draw(RenderWindow &window)
{
        for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++)
        {
                window.draw(startMenu[i]);
        }
}

main.cpp
#include <SFML/Graphics.hpp>

#include "tank.h"
#include "menu.h"

using namespace sf;

int main()
{
        RenderWindow window(VideoMode(800, 600), "Tanks v1.3");

        menu startingMenu(window.getSize().x, window.getSize().y);
       
        Tank player1;
        player1.addTankAttributes(650, 500, Color(255, 0, 0), "Player 1");
        Tank player2;
        player2.addTankAttributes(50, 500,  Color(0, 0, 255), "Player 2");

        while (window.isOpen())
        {

                Event event;
                while (window.pollEvent(event))
                {
                       

                        if (event.type == Event::Closed)
                                window.close();

                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) //move up
                {
                        player1.moveRight();
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) //move down
                {
                        player1.moveLeft();
                }

                window.clear(Color(21,196,202));
                startingMenu.draw(window);
                player1.drawTank(window);
                player2.drawTank(window);
                window.display();
        }

        return 0;
}

One more class that I've got, I tryed to use text there as well, the same problem...

Tank.h
#ifndef TANK_H
#define TANK_H

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


using namespace sf;

class Tank
{

public:
        void addTankAttributes(int x, int y, Color &color, String name);
        void drawTank(RenderWindow &window);
        void moveLeft();
        void moveRight();


        Tank();
        ~Tank();

private:
        const float rotation = 0.04;

        Texture tankTexture;
        Texture turretTexture;

        Sprite tankSprite;
        Sprite turretSprite;

        Font font;
        Text text;
};

Tank::Tank()
{
        if (!tankTexture.loadFromFile("res/tank.png"))
                std::cout << "no tank.png file" << std::endl;

        tankSprite.setTexture(tankTexture);

        if (!turretTexture.loadFromFile("res/turret.png"))
                std::cout << "no turret.png file" << std::endl;

        turretSprite.setTexture(turretTexture);
}

void Tank::addTankAttributes(int x, int y, Color &color, String name)
{
        tankSprite.setColor(color);
        tankSprite.setPosition(x, y);

        turretSprite.setColor(color);
        turretSprite.setPosition(x+15, y+2);
        turretSprite.setOrigin(0, 0);
}

void Tank::drawTank(RenderWindow &window)
{
        window.draw(turretSprite);
        window.draw(tankSprite);
}

void Tank::moveLeft()
{
        if (turretSprite.getRotation() > 152 || turretSprite.getRotation() < 17)
        {
                turretSprite.rotate(-rotation);
        }
}
void Tank::moveRight()
{
        if (turretSprite.getRotation() > 150 || turretSprite.getRotation() < 15)
        {
                turretSprite.rotate(rotation);
        }
}

Tank::~Tank()
{
}

#endif TANK_H

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #1 on: November 23, 2015, 10:21:04 pm »
The error message in the image suggests that you are trying to erase an element that doesn't exist (from a vector, which you don't seem to be using). I did find this.

On which line is this error occurring?

Could you be mixing up debug and release builds, or versions of SFML?
« Last Edit: November 23, 2015, 10:55:18 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Bellato

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #2 on: November 24, 2015, 05:10:59 am »
Thanks Hapax for reply.

When I try to Release I'm getting an error that says - "Impossible to start program. Coz there is no sfml-system-d-2.dll" (I attached screen(unfortuanetely it is in russian..., but close translation is above))

when I close this error message, I'm getting this Break Message - "Unhandled exception at 0x775C9610 (ntdll.dll) in tanks_v1.3.exe: VTGuard instrumentation code detected an attempt to use an illegal virtual function table."

Well, ok... I understand that it might miss  this SFML lib file: "sfml-system-d-2.dll", it is located in Debug folder near .exe file. (Screen Attached)

"Call Stack" doesnt send me anywhere around my code, while it is in Realse Mode.

I also attached Linker -> Input Screen, just in case

Will post all results in Debug mode in a few minutes.

Bellato

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #3 on: November 24, 2015, 05:12:01 am »
Two other screens:

Bellato

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #4 on: November 24, 2015, 05:20:52 am »
Debug Mode

Frist of all, I have forgotten to say, that the first error disapeared (Vector erase iterator outside range). I didnt do much, just tried to change code to make it workable, came back to code from my first post and oops, now there is another Break Error... (Screen Attached)

In Debug Mode it is talking about sfml-window-d-2.dll...

    sfml-window-d-2.dll!0f361bc3()   Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for sfml-window-d-2.dll]   
    sfml-window-d-2.dll!0f361a26()   Unknown
    sfml-window-d-2.dll!0f3610aa()   Unknown
    sfml-window-d-2.dll!0f35feb8()   Unknown
>   tanks_v1.3.exe!main() Line 25   C++           //this is line 25:       while (window.pollEvent(event))
    [External Code]   
« Last Edit: November 24, 2015, 05:23:34 am by Bellato »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #5 on: November 24, 2015, 03:04:52 pm »
The most obvious to note here is that you are linking the wrong libraries.
If the library filename has -d in it, it's for debug builds only. The files without -d is for release builds only.

In your property settings, you need to have different linker inputs for both debug and release so change "configuration" from "All Configurations" to the one you need to change ("Debug" or "Release")

You also need to make sure that the builds are exactly the same as the version you are "#including" and were built using exactly the correct compiler.
Are you building the libraries yourself or are you using pre-built binaries?
I would suggest possibly removing all of the versions of SFML that you may have and replacing it with just one, taking care to not have more than one.

EDIT: corrected "The files without -d is for release builds only."
« Last Edit: November 25, 2015, 03:31:37 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #6 on: November 24, 2015, 03:51:41 pm »
@Hapax:

I think you meant to say that -d is for debug only not release.(Like you said first!) ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SFML Font [Vector error] Vector erase iterator outside range
« Reply #7 on: November 25, 2015, 03:32:02 am »
Thanks, Specter! Fixed my post  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything