-
September 25th, 2012, 10:16 AM
#1
Error: no operator "==" matches these operands
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:
Code:
using namespace std;
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#include"phoneNumber.h"
class PhoneEntry
{
private:
PhoneNumber eNumber;
string firstName,
lastName;
void writeDots(ostream&, int) const;
public:
istream& readEntry(istream&);
ostream& writeEntry(ostream&) const;
bool greaterAlpha(const PhoneEntry &);
void mySort(vector<PhoneEntry>&, int);
PhoneEntry key(string, string);
};
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);
};
-
September 26th, 2012, 07:08 AM
#2
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
-
September 26th, 2012, 07:30 AM
#3
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
Code:
void mySort(vector<PhoneEntry>&, int);
Regards,
Paul McKenzie
-
September 26th, 2012, 09:45 AM
#4
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?
-
September 26th, 2012, 10:01 AM
#5
Re: Error: no operator "==" matches these operands
You add the operator as a member function.
Code:
class PhoneEntry
{
private:
PhoneNumber eNumber;
string firstName,
lastName;
void writeDots(ostream&, int) const;
public:
istream& readEntry(istream&);
ostream& writeEntry(ostream&) const;
bool greaterAlpha(const PhoneEntry &);
void mySort(vector<PhoneEntry>&, int);
PhoneEntry key(string, string);
bool operator ==(const PhoneEntry& that) const;
};
And the definition in the cpp file:
Code:
bool PhoneEntry::operator ==(const PhoneEntry& that) const
{
// return true if the member variable of this object are the same as those in 'that'
}
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
-
September 26th, 2012, 02:23 PM
#6
Re: Error: no operator "==" matches these operands
[offtopic] Hm... I actually answered to this question yesterday, but my post doesn't show up. Well, thanks again codeguru.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|