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

Author Topic: std::list::remove_if Problem.  (Read 2936 times)

0 Members and 1 Guest are viewing this topic.

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
std::list::remove_if Problem.
« on: February 29, 2012, 04:12:02 pm »
I got a little problem with using std::list::remove_if in a Class the problem is I need the function in the class. If the function are not in the class its works but i cant Destroy the Body and I need it in there to get the world.

The code.
Code: [Select]

bool Map::RemoveStructure(Structure *TheStructure){
if(TheStructure->ThisType == 2){
ThisWorld->DestroyBody(TheStructure->StructureBody);
}
return TheStructure->Remove;
}
void Map::RenderMap(sf::RenderWindow* Window){
if(sMapName.length() > 0){
for(list<Structure*>::iterator IT = StructureList.begin(); IT != StructureList.end(); IT++){
Window->Draw((*(*IT)->StructureShape));
}
StructureList.remove_if(&Map::RemoveStructure);
}
}


The error.

Code: [Select]

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1218): error C2064: Ausdruck ergibt keine Funktion, die 1 Argumente übernimmt


I hope you can help my :)
Your,
Jason :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
std::list::remove_if Problem.
« Reply #1 on: February 29, 2012, 04:37:09 pm »
A member function needs an instance of the class to be called, remove_if won't be able to call Map::RemoveStructure all alone ;)

You should have a look at boost/std::bind if you can. Or std::bind1st / std::memfun (don't remember if you need both) if you can't use boost or C++11.
Laurent Gomila - SFML developer

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
std::list::remove_if Problem.
« Reply #2 on: February 29, 2012, 05:20:26 pm »
memfun and bin1st dont work :S the same error
i dont use boost only Box2D and SFML

or how to use this functions ? it can be that i use it wrong :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
std::list::remove_if Problem.
« Reply #3 on: February 29, 2012, 05:43:42 pm »
Code: [Select]
remove_if(std::bind1st(std::mem_fun(&Map::RemoveStructure), this));
Not tested :)

Quote
the same error

An english translation would be welcome.
Laurent Gomila - SFML developer

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
std::list::remove_if Problem.
« Reply #4 on: February 29, 2012, 06:00:33 pm »
Code: [Select]
Ausdruck ergibt keine Funktion, die 1 Argumente übernimmt

hmm google translator say

Code: [Select]
Expression is not a function that takes an argument


Quote from: "Laurent"
Code: [Select]
remove_if(std::bind1st(std::mem_fun(&Map::RemoveStructure), this));
Not tested :)


I think "this" is not the variable waht i want to give with ? its the Pointer to Structure or not ?

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
std::list::remove_if Problem.
« Reply #5 on: February 29, 2012, 10:15:21 pm »
With using this in Boost

Code: [Select]
StructureList.remove_if(boost::bind(boost::mem_fn(&Map::RemoveStructure)));

I get again a Error
Quote
1>C:\....\.....\.....\Include\boost/bind/bind.hpp(182): error C2064: Ausdruck ergibt keine Funktion, die 0 Argumente übernimmt

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
std::list::remove_if Problem.
« Reply #6 on: March 01, 2012, 08:02:04 am »
Quote
I think "this" is not the variable waht i want to give with ? its the Pointer to Structure or not ?

In the end the function to call is:
Code: [Select]
bool b = this->RemoveStructure(item);
std::memfun transforms it to a regular (non-member) function call:
Code: [Select]
bool b = RemoveStructure(this, item);
And you need to fix the first argument with std::bind1st so that the final call is:
Code: [Select]
bool b = RemoveStructure(item);
... and this one is compatible with what std::list::remove_if expects.

With boost:
Code: [Select]
StructureList.remove_if(boost::bind(&Map::RemoveStructure, this, _1));

By the way, are you actually destroying the structure in your function? It should only be a predicate, ie. return a boolean indicating whether the item should be removed or not. It must not have any side-effect.
Laurent Gomila - SFML developer

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
std::list::remove_if Problem.
« Reply #7 on: March 01, 2012, 03:58:48 pm »
Thank you :)

is BOOST_FOREACH faster as a std for with iterator ?
or waht is the fastest way to run trough a list?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
std::list::remove_if Problem.
« Reply #8 on: March 01, 2012, 04:15:19 pm »
Quote
is BOOST_FOREACH faster as a std for with iterator ?

BOOST_FOREACH is a convenience macro, it just hides the iterator-based loop.

Quote
waht is the fastest way to run trough a list?

There's only one way, with iterators.
Laurent Gomila - SFML developer

Ptlomej

  • Newbie
  • *
  • Posts: 48
    • ICQ Messenger - 353167418
    • View Profile
    • Local
std::list::remove_if Problem.
« Reply #9 on: March 01, 2012, 04:31:06 pm »
Okay thank you for all :)

and.. Nice Library :) SFML 2 is very nice :)

when i find any that could be change or add
i will take up a request ;)

Closed!