Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法取消引用向量迭代器_C++_Vector_Iterator_Dereference - Fatal编程技术网

C++ 无法取消引用向量迭代器

C++ 无法取消引用向量迭代器,c++,vector,iterator,dereference,C++,Vector,Iterator,Dereference,我不明白这个函数的问题是什么,我以前做过类似的事情,效果很好,但是现在当我尝试运行这个函数时,我得到了错误 "Unable to dereference vector iterator" 它出现在curr->setName(新名称)行上这很有意义,因为这就是它被取消引用的地方。另外,为了清楚,这个方法中使用的所有函数和类都可以独立工作,为了节省空间,我不会插入它们 void System::modify(PC a){ char x; curr = find(begin(com

我不明白这个函数的问题是什么,我以前做过类似的事情,效果很好,但是现在当我尝试运行这个函数时,我得到了错误

"Unable to dereference vector iterator" 
它出现在
curr->setName(新名称)行上这很有意义,因为这就是它被取消引用的地方。另外,为了清楚,这个方法中使用的所有函数和类都可以独立工作,为了节省空间,我不会插入它们

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;
    }
}
void系统::修改(PC a){
字符x;
curr=find(开始(comps)、结束(comps)、a);

cout似乎在此语句中未找到值
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
}

目前尚不清楚如何比较PC。但似乎find函数返回end(comps),这意味着列表/向量/任何内容中都没有“PC a”。您应该检查是否已由找到PC a

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

您需要修改您的函数-它应该检查
find()
是否找到了任何东西:

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;
    }

    //...
}
void系统::修改(PC a){
字符x;
curr=find(开始(comps)、结束(comps)、a);
如果(当前==结束(补偿))
{

你能检查一下以确保
curr!=end(comps)
并且comps不是空的吗?@NathanOliver显然不是。你应该检查
curr!=end(comps)
并决定如何处理一台不存在的PC