简体   繁体   中英

error C2679: binary '==' : no operator found which takes a right-hand operand of type

I am having trouble getting my homework assignment to work properly. I have overloaded my '==' operator, but still I get this error. Not really sure why it's being thrown or how to fix it. Any help would be appreciated.

Here is my algorithm:

/* Performs a recursive binary search on the given array. It returns
 * the index of the found value, -1 otherwise. 
 */
template <typename T, typename V>
int binarySearch(T* list[], const V& searchValue,
                 const int firstIndex, const int lastIndex) 
{ 
    if (firstIndex <= lastIndex) 
    {
        int mid = (firstIndex + lastIndex) / 2;  //mid point of list.
        if (searchValue == *list[mid]) 
            return mid;   // found value.
        else if (searchValue < *list[mid]) 
            return binarySearch(list, firstIndex, mid - 1, searchValue);
        else
            return binarySearch(list, mid + 1, lastIndex, searchValue);
    }
    return -1;    //failed to find value
}

Debugger says this line in main is where the error originates:

// Search the person array.
cout << "Searching people array for person with name = 'Mickey Mouse': "
     << (binarySearch(person, "Mickey Mouse", 0, 7) != -1? "found it." : "did not find it.")
     << endl;

Here is my person class header file showing the overloaded operator:

#ifndef PERSON_H
#define PERSON_H

#include <string>
#include <iostream>

using namespace std;

namespace P03 {
class Person {...}; // end Person


/* Displays a Person to the screen.
 * Calls the toString() method.
 */
ostream& operator <<(ostream& out, const Person& person)
{
    return out << person.toString();
}

/* The following relational operators compare two instances of the
 * Person class. The comparison is made on the compound value of:
 * <lastName><space><firstName>
 */
bool operator ==(const Person& lhs, const Person& rhs)
{
    return lhs.getName() == rhs.getName();
}

    /*other operators*/
    ...

} // end namespace P03

#endif

Not sure if more of my code is needed. I will update if need be.

When you call

binarySearch(person, "Mickey Mouse", 0, 7)

In binarySearch , T the type of which person is an array of pointers, and V is const char* . Then in the body you do

searchValue == *list[mid]

Which is const char*& == *person[x] , which is why you get the error since there's no operator==(const char*, X) where X is whatever *person[x] is.

Your template class is for types T and V . In your binarySearch function, you take a list of type T and a search value of type V . You then compare them: if (searchValue == *list[mid]) . This is where the error is, because you probably have not implemented an == operator for class T which takes in an argument of type V .

The problem can be traced back to your cout , where you pass in Person as type T and a const char* as type V . Your Person class' == operator only takes in a right-hand operand also of type Person . In other words, in the expression a == b , b must be the Person type.

The line if (searchValue == *list[mid]) compares the types const V& with a T. V is a C-string ( char* ), and assuming that person is an array of Person* T is a Person . You have provided a const Person&, const Person& comparison operator, but the code expects a const char*&, const Person comparison operator. Either provide such an operator, or create a Person from the string in the binarySearch(person, "Mickey Mouse", 0, 7) expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM