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

Author Topic: SFML and C++0x; "error: use of deleted function"  (Read 7552 times)

0 Members and 1 Guest are viewing this topic.

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
SFML and C++0x; "error: use of deleted function"
« on: October 26, 2011, 08:49:52 pm »
Hi there!

As some of you may know I'm still a beginner with C++, so sometimes I got newbie questions.
Let's suppose, I have this object:

Code: [Select]
class aClass
{
public:
   aClass () { // initialize stuff }

    void Update () { // update stuff, not relevant }

    void Draw (const sf::RenderTarget& target)
    {
         Sprite.Draw(target);
    }

private:
    sf::Sprite Sprite;
};


I have a few of this object in a vector:
Code: [Select]
std::vector<aClass> vec;

So, I was experimenting with for_each and std::mem_fun_ref, and I came up with this:

Code: [Select]

// win is a sf::RenderWindow object
for_each(vec.begin(), vec.end(), std::mem_fun_ref(&aClass::Update));

for_each(vec.begin(), vec.end(), std::bind(std::mem_fun_ref(&aClass::Draw), win));


I compile it with -std=c++0x using g++ (4.6, so it supports C++0x), under Linux, and I got a nice error message, "error: use of deleted function" at the second line. After a bit of Google, I really didn't get a good answer. I think, it is because the sf::RenderWindow is a non-copyable class, which is the base class of sf::RenderTarget. Is this correct ? Or I'm doing it in a wrong way ?
Because, if I use a "classic" for loop, it compiles just fine. (note: the first line compiles without any error, and does what it should do in both cases)
Code: [Select]

std::vector<aClass>::iterator iter;
for (iter = vec.begin(); iter != vec.end(); ++iter)
{
   iter->Draw(win);
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML and C++0x; "error: use of deleted function"
« Reply #1 on: October 26, 2011, 09:01:26 pm »
Try to pass std::ref(win) instead of win. So that it is not copied.
Laurent Gomila - SFML developer

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
SFML and C++0x; "error: use of deleted function"
« Reply #2 on: October 30, 2011, 03:25:11 am »
That seems to be not working. Here is a minimal example.

Code: [Select]
#include <functional>
#include <vector>

#include <SFML/Graphics.hpp>

class entity
{
public:
entity (int x)
{
sf::FloatRect rect(x, 50, 50, 50);
Shape = sf::Shape::Rectangle(rect, sf::Color::Red);
}

void update ()
{
}

void draw (sf::RenderTarget& target)
{
target.Draw(Shape);
}

private:
sf::Shape Shape;

};

int main ()
{
std::vector<entity> ents;

sf::RenderWindow win(sf::VideoMode(640, 480, 32), "test", sf::Style::Close);

entity en1(0);
entity en2(50);
entity en3(150);

ents.push_back(en1);
ents.push_back(en2);
ents.push_back(en3);

while (win.IsOpened())
{
sf::Event event;

while (win.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
win.Close();
}

win.Clear(sf::Color::Black);

for_each(ents.begin(), ents.end(), std::mem_fun_ref(&entity::update));

for_each(ents.begin(), ents.end(), std::bind(std::mem_fun_ref(&entity::draw), std::ref(win)));

win.Display();
}

return 0;
}


It still won't compile. I get a different error, which is....cryptic to me :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML and C++0x; "error: use of deleted function"
« Reply #3 on: October 30, 2011, 09:24:14 am »
Quote
I get a different error

Which is... ? ;)

And why are you still using std::mem_fun_ref if you have the almighty std::bind?
I can't try, but something like this should work:
Code: [Select]
for_each(ents.begin(), ents.end(), std::bind(&entity::update, _1));

for_each(ents.begin(), ents.end(), std::bind(&entity::draw, _1, std::ref(win)));
Laurent Gomila - SFML developer

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
SFML and C++0x; "error: use of deleted function"
« Reply #4 on: November 03, 2011, 08:02:13 am »
Quote from: "Laurent"
Quote
I get a different error

Which is... ? ;)

And why are you still using std::mem_fun_ref if you have the almighty std::bind?
I can't try, but something like this should work:
Code: [Select]
for_each(ents.begin(), ents.end(), std::bind(&entity::update, _1));

for_each(ents.begin(), ents.end(), std::bind(&entity::draw, _1, std::ref(win)));


Thanks a lot, it compiles now :) Is there any beginner friendly documentation about the new features of C++? I can't find one. I checked the gcc one, but it's a bit hard for me to read.

I'm sorry, but I can't copy the error message :(

Nehmulos

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • http://twitter.com/Nehmulos
SFML and C++0x; "error: use of deleted function"
« Reply #5 on: November 03, 2011, 09:12:14 pm »
Quote from: "unranked86"

 Is there any beginner friendly documentation about the new features of C++? I can't find one. I checked the gcc one, but it's a bit hard for me to read.


The English wikipedia has a very nice article, which contains many examples, about the new features in C++11 (former C++0x) see: http://en.wikipedia.org/wiki/C%2B%2B11

ratzlaff

  • Newbie
  • *
  • Posts: 33
    • View Profile
SFML and C++0x; "error: use of deleted function"
« Reply #6 on: November 03, 2011, 10:08:33 pm »
There are some good articles over at codeproject that highlight the new features of c++11

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
SFML and C++0x; "error: use of deleted function"
« Reply #7 on: November 05, 2011, 02:49:08 pm »
C++Next has some good articles (especially concerning move semantics), too.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: