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

Author Topic: draw vector class - problem  (Read 1516 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
draw vector class - problem
« on: June 12, 2015, 04:22:28 pm »


I have some issue with drawing vectror of class containing shape objects.
Below sample code:

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

class Block
        sf::RectangleShape rectangle;
        sf::RectangleShape create ( int xPos, int yPos );

sf::RectangleShape Block::create ( int xPos, int yPos )
    sf::RectangleShape object( sf::Vector2f ( 120, 50 ) );
    object.setPosition( xPos, yPos );
    return object;

int main()
   sf::RenderWindow window;
   window.create(sf::VideoMode(800, 600), "Sample application");
   std::vector < Block > blocks;
   Block *ptr;
   for (int i = 0; i < 10; i++)
        ptr = new Block;
        ptr->create( i*2, i*2 );
   delete ptr;
   ptr = NULL;
       //Event Handler
        sf::Event event;
        while(window.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {


        for ( int i = 0; i < blocks.size(); i++ ) {
            window.draw( blocks[i] ); /** ERROR */


Qustion is why am I getting an error?

In advance, thank You for answer   :)


  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: draw vector class - problem
« Reply #1 on: June 12, 2015, 04:35:14 pm »
You need to make your block class inherit from `sf::Drawable`. See the tutorial: http://www.sfml-dev.org/tutorials/2.3/graphics-vertex-array.php#creating-an-sfml-like-entity

Also, your `create`function doesn't modify your current Block instance (and its `rectangle` attribute). That's most probably not what you want to do.

Additionally, you have some huge issue with memory in this code. As an exercise, try to find out, without running it, where you do something fishy about your manual allocation. Once you have understood that, read about RAII. And for what you're doing, you actually don't need new/delete: use stack-allocated object directly. (That's just some pointers to help you improve your C++ skills.)

PS: Next time, please include the full error message you're having.
SFML / OS X developer


  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: draw vector class - problem
« Reply #2 on: June 15, 2015, 10:50:20 am »
Looking at my previous post I see how little information I’ve provided (writing posts in a hurry is a bad idea  it seems  :) ).

The main idea of this exercise was to try using vector class congaing “RectangleShape” type objects.
I’ve managed to store them in array, but it’s not an efficient way (especially when object needs to be destroyed).

Thank You for time, effort and value tips – having them in mind, I’ll try to figure this out.