-1

I keep getting "vector iterators incompatible" for executing following code:

Tag Som::UpdateAfterTagMessageExciter(string detectedTagID, unsigned char ReaderID, unsigned char antData, SSensors Sensors,bool staff, int BatLevel, int RSSI, int temperature)
{

        tagsIT = search (m_DetectedTags.begin(), m_DetectedTags.end(), searchTag,              
                             searchTag+1,TagCollection::SearchTagsCollByTagIDPredicate);

        if (tagsIT<m_DetectedTags.end())
        {
                 //do something
        }

the SearchTagsCollByTagIDPredicate is as following:

static bool SearchTagsCollByTagIDPredicate (Tag i, string tagID) 
     {
        string secondTag=  i.GetTagID();
        return (tagID==secondTag);
      }

Where m_DetectedTags vector is not used outside this block. The function is being called repeatedly each time a tag is detected by LF antenna. It happens right in the call to search. I tried debugging but to no avail.

I tried replacing the search with a simple for loop and it still doesn't work.

Any ideas why the search is causing the error? Thanks!

user1997268
  • 287
  • 1
  • 6
  • 15
  • 3
    You should try to reduce this to a minimal code sample that reproduces the problem. But the error usually means you are comparing iterators from different vectors. – juanchopanza Aug 29 '13 at 13:31
  • Are you getting a compile error? If so, on which line? – Vaughn Cato Aug 29 '13 at 13:33
  • no - not getting a compile error. getting the error on run time and not immediately when running the code but only after a while. – user1997268 Aug 29 '13 at 13:36
  • Also, you should penalize the author for using leading underscores and, if not introduced by copying the code, horrible formatting. – thokra Aug 29 '13 at 13:37
  • sorry about the way I've put the code. Basically I believe the part of code which cause the error: tagsIT = search (_DetectedTags.begin(), _DetectedTags.end(), searchTag, searchTag+1,TagCollection::SearchTagsCollByTagIDPredicate); – user1997268 Aug 29 '13 at 13:48
  • @thokra: What's so bad about leading underscores? I use them for my private members. – Adrian Willenbücher Aug 29 '13 at 13:54
  • 2
    @AdrianWillenbücher, an underscore followed by uppercase letter is a [reserved name](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Jonathan Wakely Aug 29 '13 at 13:58
  • Why are you using `tagID.compare(secondTag)==0` instead of `tagID == secondTag`? – Jonathan Wakely Aug 29 '13 at 14:00
  • @thokra, no, please read the link in my comment, `_this` is OK except at global scope, `_This` and `__this` are not. – Jonathan Wakely Aug 29 '13 at 14:00
  • @JonathanWakely: Oops, my bad. Forgot about that. Thx for the reminder. Deleted my original comment due to inaccuracy. – thokra Aug 29 '13 at 14:02
  • @user1997268: have you tried searching for the error message? A quick Google search found this: [#1](http://stackoverflow.com/questions/8421623/vector-iterators-incompatible) [#2](http://stackoverflow.com/questions/6187835/vector-iterators-incompatible) [#3](http://stackoverflow.com/questions/5762933/vector-iterators-incompatible-error). Did none of those help you? – Adrian Willenbücher Aug 29 '13 at 14:04
  • @AdrianWillenbücher: Yes - I did try searching google, and no - it didn't help.. – user1997268 Aug 29 '13 at 14:14

2 Answers2

0

Is your SearchTagsCollByTagIDPredicate function, a method of TagCollection class for which you have put the definition in the TagCollection class header file or just a static function?It is not clear from the context. May be that is what is causing the problem.

dhanushka
  • 10,492
  • 2
  • 37
  • 47
-1

There is not much code to go with, so the only thing I can see that is out of the ordinary is the following:

  • You are using < to compare iterators. Don't know if that is legal for vector iterators. But typically one would write:

    if (tagsIT != _DetectedTags.end())

  • You are passing the parameters to your comparator function by value, and not by reference. If your copy constructor or destructor for Tag is buggy, your problem may lie there.