Search code examples
c++listhashstructurechaining

How to solve C2676 operator error while using c++ Chain Hash with structure?


I get an error that says Error C2676 binary '==': 'student' does not define this operator or a conversion to a type acceptable to the predefined operator Project_datastr2 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xutility Line:5440

When I click on the error link it opens an description which is about operators. But I could not understand what should i do. If anyone can send me a repyl I would be very glad.

My class and structure decleration

#pragma once
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <cstdlib>

using namespace std;
struct student {
    long id; // 9-digit student id,
    char name[30]; //student name, surname
    char address[50]; //address of the student
    double gpa;
};

class Hash
{
public:
    Hash();
    Hash(int htype,int tsize);
    void insert(struct student std);
    void remove(struct student std);
    int hash1(long key);
    int hash2(char key[30]);
    int hash3(char key[50]);
    bool search(struct student std);
    void destroy();
    void display();
    int hash_template(struct student std);
    void utilization(); 
private:
    int size; //prime number for more unique results
    list<struct student> *hashTable;
    int hashType;
};

My constructors:

Hash::Hash()
{
    this->size = 11;
    this->hashType = 1;
    hashTable = new list<struct student>[this->size];
}

Hash::Hash(int htype,int tsize)
{
    this->size = tsize;
    this->hashType = htype;
    hashTable = new list<struct student>[this->size];
}

My other functions which I suspect that causes the error.

void Hash::insert(struct student std)
{
    bool isExist = search(std);
    if (!isExist) 
    {
    int tableIndex = hash_template(std);
    hashTable[tableIndex].push_back(std);
    }       
}

void Hash::remove(struct student std)
{
    int index;
    if (search(std))
    {
        switch (hashType)
        {
        case 1:
            index = hash1(std.id);
        case 2:
            index = hash2(std.address);
        case 3:
            index = hash3(std.name);
        default:
            index = hash1(std.id);
            break;
        }
        auto it = find(hashTable[index].begin(), hashTable[index].end(), std);
        if (it == hashTable[index].end())
            return;
        hashTable[index].erase(it);
    }
    else
        cout << "Error! Cannot remove !" <<std.name << " is not in the list!" << endl;
}

bool Hash::search(struct student std)
{   
    int index;
    switch (hashType)
    {
    case 1:
        index = hash1(std.id);
        break;
    case 2:
        index = hash2(std.address);
        break;
    case 3:
        index = hash3(std.name);
        break;
    default:
        cout << "search() default state" << endl;
    }   
    auto it = find(hashTable[index].begin(), hashTable[index].end(), std);
    if (it == hashTable[index].end())
        return false;
    return true;
}

void Hash::display()
{
    for (int i = 0; i < size; i++)
    {
        cout << i << " -> ";
        for (auto itr = hashTable[i].begin(); itr != hashTable[i].end(); itr++)
        {
            cout << itr->name << " -> ";
        }
        cout << endl;
    }
}

int Hash::hash_template(struct student std)
{   
    int index;
    switch (hashType)
    {
    case 1:
        index = hash1(std.id);
        break;
    case 2:
        index = hash2(std.address);
        break;
    case 3:
        index = hash3(std.name);
        break;
    default:
        cout << "hashTemplate default state" << endl;
    }
    return index;
}

My main code is:

#include "Hash.h"

int main()
{   
    struct student ilker = {16000214,"İlker Kılınçarslan","Bahçeşehir",2.5};
    struct student safa = {160009999,"Safa Orhan","Beylikdüzü",2.5 };
    Hash h1(1, 11);
    h1.insert(ilker);
    h1.display();
    return 0;
}

The error message:

C2676 binary'==':'student' does not define this operator or a conversion to a type acceptable to the predifined operator File xutiliy Line 5440


Solution

  • This is not the complete answer but you can add the friend function:

    struct student
    {
       long id;           // 9-digit student id,
       char name[30];     // student name, surname
       char address[50];  // address of the student
       double gpa;
       friend bool operator==(student const&, student const&) = default;
    };
    

    This assumes C++20.

    If C++20 is not in range try:

       friend bool operator==(student const& a, student const& b)
       {
          return a.id == b.id && std::strcmp(a.name, b.name) == 0 &&
                 std::strcmp(a.address, b.address) == 0 && a.gpa == b.gpa;
       }
    

    Or adjust according to your notion of equality between students. It might be just

       friend bool operator==(student const& a, student const& b)
       {
          return a.id == b.id;
       }