I'm trying to do a binary search on a vector of strings and keep getting this error. I just can't for the life of me figure out what is wrong, so if anyone could help me out that'd be really amazing.
This is the PhoneEntry header file with the class declaration:
And here's the part of the program that I'm having issues with:
Code:
int binarySearch(PhoneEntry keyP, vector<PhoneEntry> entryNames)
{
int low = 0, mid, high = entryNames.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (keyP.greaterAlpha(entryNames[mid]))
//greaterAlpha compares the two strings and returns true or false based on alphabetical order
high = mid - 1;
else if (!keyP.greaterAlpha(entryNames[mid]))
low = high + 1;
else
low = high + 1;
}
if (keyP == entryNames[mid]) //This is the line where I keep getting the error
return mid;
else
return (mid * -1);
};
Re: Error: no operator "==" matches these operands
Originally Posted by taymaxi
I'm trying to do a binary search on a vector of strings and keep getting this error. I just can't for the life of me figure out what is wrong, so if anyone could help me out that'd be really amazing.
Unless this is a homework assignment, why don't you use std::binary_search or std::set?
Code:
int binarySearch(PhoneEntry keyP, vector<PhoneEntry> entryNames)
Why do you pass these arguments by value? You should pass them by const reference, as they are not built-in types and thus expensive to copy.
Code:
{
int low = 0, mid, high = entryNames.size()-1;
while (low <= high)
{
mid = (low + high) / 2;
if (keyP.greaterAlpha(entryNames[mid]))
//greaterAlpha compares the two strings and returns true or false based on alphabetical order
high = mid - 1;
else if (!keyP.greaterAlpha(entryNames[mid]))
low = high + 1;
else
low = high + 1;
}
greaterAlpha returns a bool. That can have only two values: true or false. So how come you have three options?
Code:
if (keyP == entryNames[mid]) //This is the line where I keep getting the error
return mid;
else
return (mid * -1);
};
Operator == is not overloaded for PhoneEntry. You have to define an overload for this operator if you want to use it.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Re: Error: no operator "==" matches these operands
Originally Posted by taymaxi
I'm trying to do a binary search on a vector of strings and keep getting this error. I just can't for the life of me figure out what is wrong, so if anyone could help me out that'd be really amazing.
In addition to what was stated (why are you not using std::binary_search?), hopefully you're using std::sort to do this instead of writing the sorting code yourself
Re: Error: no operator "==" matches these operands
This is a homework assignment, I'm supposed to be writing the code myself. I'm fairly new to all this. How do I overload the == operator for PhoneEntry?
Bookmarks