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

Author Topic: Segmentation Fault with vector<Sprite>  (Read 3006 times)

0 Members and 1 Guest are viewing this topic.

Tarbal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Segmentation Fault with vector<Sprite>
« on: October 12, 2016, 11:14:24 pm »
I'm not a c++ wizard, but I'm pretty sure this is an issue with SFML itself. I kept it really basic, but I'm having trouble with vector<Sprite>. Do I really have to list them all explicitly? A loop seems easiest, so that's how I tried, but I don't think push_back is working, according to my gdb debugger. As the subject says, I get a segmentation fault upon compilation of the following code:

#ifndef events_H
#define events_H

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

class LiveEvents
{
public:
        void Switchboard();
private:
        union switches;
};

#endif

 


#include <events.hpp>

void LiveEvents::Switchboard()
{
        sf::Event event;
        switch (event.type)
        {
                case sf::Event::KeyPressed:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                                // do stuff                    
                break;

        }
        switch (event.type)
        {
                case sf::Event::KeyPressed:
                        if(event.key.code == sf::Keyboard::C)
                                //do stuff
                break;

        }

}

 

And the main loop:

#include <events.hpp>
#include <vector>
int main()
{
        sf::RenderWindow screen(sf::VideoMode(800,600), "Window");
        screen.setVerticalSyncEnabled(true);
        screen.setFramerateLimit(60);
        while (screen.isOpen())
        {
                sf::Event screen_event;
                while(screen.pollEvent(screen_event))
                {
                        switch(screen_event.type)
                        {
                                case sf::Event::Closed:
                                        screen.close();
                                default:
                                break;
                        }
                        LiveEvents switches;
                        switches.Switchboard();
                }
                screen.clear(sf::Color::Black);

                sf::Texture background;
                if(!background.loadFromFile("../textures/background_rect.png"))
                {
                        return -1;
                }
                sf::Sprite sprite;
                sprite.setTexture(background);
                int width = sprite.getGlobalBounds().width;
                int height = sprite.getGlobalBounds().height;
                std::vector<sf::Sprite> sprite_vector;
                for(int i=0; i<3; i++)
                {
                        sprite_vector[i].push_back(sprite);
                }
                for(int i=0; i<3; i++)
                {
                        for(int j=0; j<3; j++)
                        {
                                sprite_vector[i].setPosition(sf::Vector2f(i*width,j*height));
                        }
                }
                for(int i=0; i<3; i++)
                {
                        screen.draw(sprite_vector[i]);
                }

                screen.display();
        }
}

 

Is there a better way to handle containers than vector<Sprite>? As it looks right now, I'll have to list all of them explicitly, and no way am I doing that. I've seen a few forum posts that say do this or that, but no one shows a simple example of how to display multiple sprites. Can someone please show me and/or point out what's wrong with my code?

edit: left out the push_back loop on accident. I must have changed something, but now I get

error: ‘__gnu_cxx::__alloc_traits<std::allocator<sf::Sprite> >::value_type’ has no member named ‘push_back’
    sprite_vector[i].push_back(sprite);
 
« Last Edit: October 12, 2016, 11:41:11 pm by Tarbal »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Segmentation Fault with vector<Sprite>
« Reply #1 on: October 12, 2016, 11:32:27 pm »
Quote
I'm pretty sure this is an issue with SFML itself
No it isn't. It is an issue with your lack of knowledge about std::vector and containers in general ;)

Look at what you do:
- you add 1 element to the vector
- you set the position of 3 elements in the vector
- you draw 5 elements from the vector

So you basically have 6 out-of-bounds access in the vector.
Laurent Gomila - SFML developer

Tarbal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Segmentation Fault with vector<Sprite>
« Reply #2 on: October 12, 2016, 11:33:45 pm »
Yeah, I just edited. I must have changed something. I didn't mean SFML was wrong, I meant I don't understand containers in SFML

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Segmentation Fault with vector<Sprite>
« Reply #3 on: October 12, 2016, 11:34:39 pm »
Ok now that you've changed your code in your first post, you still draw 5 sprites when you only have 3 in the vector. And you try to call push_back() on elements of your vector, ie. sf::Sprite instances.

Please please don't try to write random stuff, learn how to use std::vector and C++ properly.
Laurent Gomila - SFML developer

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Segmentation Fault with vector<Sprite>
« Reply #4 on: October 12, 2016, 11:38:39 pm »
If you for some reason need to declare a vector inside a function (and don't want to create it again and again) use the 'static' keyword.

But first you must follow the Laurent's advice:

Quote
Please please don't try to write random stuff, learn how to use std::vector and C++ properly.
I would like a spanish/latin community...
Problems building for Android? Look here

Tarbal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Segmentation Fault with vector<Sprite>
« Reply #5 on: October 12, 2016, 11:47:42 pm »
I was changing stuff at the last second before I posted and went from all 5's to 3's, but missed one. Was trying to edit in here, but didn't work out so well. In any case, it's the

sprite_vector[i].push_back(sprite)
 

that I don't understand why it doesn't work.

Any help would be greatly appreciated.

Tarbal

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Segmentation Fault with vector<Sprite>
« Reply #6 on: October 12, 2016, 11:51:06 pm »
OK, it's working now that I took out the [i]. Sorry if I wasted your time.
« Last Edit: October 13, 2016, 08:15:16 am by Laurent »

 

anything