无法取消引用矢量迭代器

时间:2015-04-24 18:09:37

标签: c++ vector iterator dereference

我不明白这个功能的问题是什么,我过去做过类似的事情并且工作正常,但现在当我尝试运行此功能时,我得到了错误

"Unable to dereference vector iterator" 

它出现在curr->setName(new_name);行,因为它被解除引用的地方是有意义的。另外要清楚这个方法中使用的所有函数和类都可以自行工作,我只是为了空间而不插入它们。

void System::modify(PC a){
    char x;
    curr = find(begin(comps), end(comps), a);

    cout << "What would you like to modify:" << endl;
    cout << "a - Name" << endl;
    cout << "b - IP" << endl;
    cout << "c - Password" << endl;

    cin >> x;
    if(x == 'a'){
        string new_name;
        cout << "What would you like to rename the computer to: ";
        cin >> new_name;
        curr->setName(new_name);
    }

    if(x == 'b'){
        string new_IP;
        cout << "What would you like the new IP address to be: ";
        cin >> new_IP;
        curr->setIP(new_IP);
    }

    if(x == 'c'){
        curr->setNewPass();
    }

    else{
        cout << "Choice is not valid" << endl;
        return;
    }
}

3 个答案:

答案 0 :(得分:2)

你需要修改你的功能 - 它应该检查 {"id":"1349306","categoryName":"Kids","imageSource":"7/optimized/8769127_fpx.tif","swatchColorList":[{"Natural Burlap":"8/optimized/8769128_fpx.tif"},{"Navy":"5/optimized/8748315_fpx.tif"},{"Red":"8/optimized/8748318_fpx.tif"}],"suppressColorSwatches":false,"primaryColor":"Natural Burlap","clickableSwatch":true,"selectedColorNameID":"Natural Burlap","moreColors":false,"suppressProductAttribute":false,"colorFamily":{"Natural Burlap":"Ivory/Cream"},"maxQuantity":6} 是否找到了一些东西:

find()

find()的文档页面说:

  

返回值

     

比较等于val的范围中第一个元素的迭代器。 如果没有元素匹配,则函数返回void System::modify(PC a){ char x; curr = find(begin(comps), end(comps), a); if(curr == end(comps)) { cout << "Specified PC was not found!" << endl; return; } //... }

在这种情况下,lastlast

答案 1 :(得分:0)

似乎在此声明中找不到值a

curr = find(begin(comps), end(comps), a);

curr等于end(comps)

您应该检查搜索是否成功。

例如

if ( ( curr = find(begin(comps), end(comps), a) ) != end(comps) )
{
    // the search was successfull
}

答案 2 :(得分:0)

目前尚不清楚PC如何进行比较。但似乎find函数返回end(comps),这意味着没有&#34; PC a&#34;在列表/矢量/无论如何。您应该检查

找到了PC a
if(curr!=end(comps)) {
// do whatever you wont with corr
}
else {
//nothing has been found
}