0

I have a set, and in that set are struct objects. Here is my set declaration:

//Where tile is the struct I'm using
set<tile> puzzleSet;

Suppose I want the tile object only. How can I do this or is it not even possible? I've tried doing:

set<tile>::iterator it = puzzleSet.find(myTile);
tile yourTile = *it;

but this just gives an error saying set iterator is not dereferencable. Thanks in advance!

  • 3
    Could we see the full error message? And are you sure `it != puzzleSet.end()`? – David G Sep 26 '18 at 00:05
  • @Havenard *it is the standard c++ overload of dereferncing an iterator – pm100 Sep 26 '18 at 00:06
  • 1
    Well, sorry then. i think i've been away from C++ for too long. – Havenard Sep 26 '18 at 00:07
  • @0x499602D2 Providing a screenshot of the error message isn't necessary. All that is said is "Expression: map/set iterator not dereferencable" – Matthew Vanlandingham Sep 26 '18 at 00:08
  • All good @Havenard – Matthew Vanlandingham Sep 26 '18 at 00:09
  • 3
    `find` returns the `end` iterator if the item was not found in the `set`. Dereferencing `end` is not a good idea ([Undefined Behaviour](https://en.wikipedia.org/wiki/Undefined_behavior)). It looks like your tool chain could be warning you. Quite nice of them. – user4581301 Sep 26 '18 at 00:10
  • @user4581301 That would make sense, but it is finding the item in the set. If I do 'it->id' (where id is part of the struct), it prints out the correct information. – Matthew Vanlandingham Sep 26 '18 at 00:20
  • That's bizarre because `->` should be doing the same thing as `*` plus a little bit more. Then again, that's UB. – user4581301 Sep 26 '18 at 00:26
  • 1
    @MatthewVanlandingham: You might want to try to insert the check for `end` anyway; the warning might be because failure is *possible* (after all, at compile time, it can't know if failure *will* occur). Nothing you're doing seems problematic by the spec, assuming `tile` has a legal copy constructor (move constructor won't do it, given `set` iterators iterate over `const` values, and therefore can't move from anything in the `set`), so the main thing is to ensure your code isn't doing anything that might invoke undefined behavior, allowing the compiler to give you nasal demons and the like. – ShadowRanger Sep 26 '18 at 00:28
  • @ShadowRanger One last thing, the error is a runtime error. it compiles just fine. Also, your comment about the copy constructor got me thinking, so I implemented one into my struct, but it didn't help unfortunately. – Matthew Vanlandingham Sep 26 '18 at 00:36
  • @MatthewVanlandingham: Typically, you should be using [the Rule of Zero](http://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero) so the compiler generated copy constructor is used. If that's the case, defining a copy constructor actually makes things worse, by disabling all the other automatic constructor and assignment overloads. I was only mentioning the copy constructor because a deleted copy constructor (including implicitly, e.g. if a member was a `std::unique_ptr`) would make the code invalid, not suggesting you write one. Rule of Zero is golden, stick to it. – ShadowRanger Sep 26 '18 at 00:48
  • Please provide a [mcve]. Guessing games help no one. – super Sep 26 '18 at 07:41

0 Answers0