简体   繁体   中英

C++ Vector iterators incompatible

Hi i have problem when run my programm. Have exception "Vector iterators incompatible" on this part of code backtrack(params, set, results);

Full code u can see on this link http://liveworkspace.org/code/MjgyND $7

ps > On MacOS in XCode all working fine, but on VS 2012 (Win7) i have this error..

ps > On liveworkspace work fine. May be need modify compiler settings?

int backtrack(btIData params, std::vector<float> set, std::vector<btNode> &results)
{
    if (reject(params, set)) {
        return 0;
    } else {
        accept(params, set, results);
    }

    set = first(params,set);
    while( (set.size() != 0) || reject(params, set)) {
        backtrack(params, set, results);
        set = right(params,set);
    }

    return 0;
}

Well, did you try to use a debugger ? If so, what did you find? If not, then this is not exactly a "debug my code for me" web site.

Anyway, it is hard to figure out what your code is doing without additional knowledge of application area. And it is pretty messy to debug, since you pass a lot of containers by value.

However, one formal error is fairly obvious. Your right and first functions will grow the set array (from backtrack ) to the greater size than the size of params.input array. Eg if your params.input array has size 5 (as in your test code), your set array will grow to size 6 .

This condition in both functions was apparently supposed to restrict the growth of set array

int l = (int) candiates.size(); // `candiates` is `set`
if (l > params.input.size())
  // Don't grow array
else
  // Grow array

but for some reason you used strict comparison l > params.input.size() instead of non-strict one l >= params.input.size() . This is exactly what allows your set array to grow to size 6 , when params.input has only 5 elements.

Then later in getPathSummary you iterate over the input array with index value from 0 to sets.size() - 1

float getPathSummary(btIData params, std::vector<float> sets)
{
    float summary = 0;
    for (int i =0; i < sets.size(); i++) {
        summary += params.input[i] * sets[i];
    }

    return summary;
}

which causes the index to go out of range and the program to crash. Ie you attempt to access params.input[5] , which does not exist.

Out-of-bound access attempts will produce different run-time errors in different debug implementations of standard library. In your case it just happened to be something about "incompatible iterators".

PS Stop passing around heavy data structures by value. Use references.

If the class btNode is defined in another DLL and the template std::vector gets intanciated in that DLL, you might have incompatibilities depending on the version of the standard library used to build your code and the one used to build the external DLL.

But in your case everything seems to live in the same file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM