CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2012
    Posts
    12

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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Error: no operator "==" matches these operands

    Quote Originally Posted by taymaxi View Post
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error: no operator "==" matches these operands

    Quote Originally Posted by taymaxi View Post
    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

  4. #4
    Join Date
    Sep 2012
    Posts
    12

    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?

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured