Reputation: 333
I don't understand what the problem with this function is, I've done something very similar to this in the past and it worked fine, but now when I try to run this function I get the error
"Unable to dereference vector iterator"
It comes on the line curr->setName(new_name);
which makes sense since that's where its being dereferenced. Also just to be clear all the functions and classes used in this method work fine on their own, I'm just not inserting them for the sake of space.
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;
}
}
Upvotes: 0
Views: 201
Reputation: 12058
You need to modify your function - it should check, whether find()
has found something at all:
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;
}
//...
}
Documentation page for find() says:
Return value
An iterator to the first element in the range that compares equal to val. If no elements match, the function returns
last
.
Where last
in this case is end(comps)
.
Upvotes: 2
Reputation: 310930
It seems that value a
was not found in this statement
curr = find(begin(comps), end(comps), a);
and curr
is equal to end(comps)
.
You should check that the search was successfull.
For example
if ( ( curr = find(begin(comps), end(comps), a) ) != end(comps) )
{
// the search was successfull
}
Upvotes: 0
Reputation: 72
it's not clear how the PC could be compared. But it seems that find function returns end(comps), that means that there is no "PC a" in the list/vector/whatever. You should check that PC a has been found by
if(curr!=end(comps)) {
// do whatever you wont with corr
}
else {
//nothing has been found
}
Upvotes: 0