简体   繁体   English

C ++矢量迭代器不兼容

[英]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); 代码backtrack(params, set, results);这一部分有异常“ Vector迭代器不兼容” backtrack(params, set, results);

Full code u can see on this link http://liveworkspace.org/code/MjgyND $7 您可以在此链接上看到完整的代码http://liveworkspace.org/code/MjgyND $ 7

ps > On MacOS in XCode all working fine, but on VS 2012 (Win7) i have this error.. ps>在XCode的MacOS上一切正常,但是在VS 2012(Win7)上我遇到此错误。

ps > On liveworkspace work fine. ps>在liveworkspace上工作正常。 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. rightfirst功能将成长set阵列(从backtrack )比规模更大的大小params.input阵列。 Eg if your params.input array has size 5 (as in your test code), your set array will grow to size 6 . 例如,如果您的params.input数组的大小为5 (如测试代码中一样),则您set数组将增长为6

This condition in both functions was apparently supposed to restrict the growth of set array 这两个函数中的这种情况显然应该限制set数组的增长

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() . 但是由于某种原因,您使用了严格的比较l > params.input.size()而不是非严格的l >= params.input.size() This is exactly what allows your set array to grow to size 6 , when params.input has only 5 elements. params.input只有5元素时,这正是使您的set数组增大到6

Then later in getPathSummary you iterate over the input array with index value from 0 to sets.size() - 1 然后,稍后在getPathSummary ,对索引值从0sets.size() - 1input数组进行迭代。

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. 也就是说,您尝试访问不存在的params.input[5]

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. PS停止按值传递繁重的数据结构。 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. 如果在另一个DLL中定义了btNode类,并且该DLL中隐含了模板std :: vector,则可能不兼容,具体取决于用于构建代码的标准库的版本和用于构建外部DLL的标准库的版本。

But in your case everything seems to live in the same file 但是在您的情况下,所有内容似乎都驻留在同一文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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