使用带结构的 C++ Chain Hash 时如何解决 C2676 运算符错误?

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

我收到一条错误消息 错误 C2676 二进制“==”:'student' 未定义此运算符或转换为预定义运算符可接受的类型 Project_datastr2 C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC .28.29333\include\xutilityLine:5440

当我点击错误 link 时,它会打开一个关于运算符的描述。但我不明白我该怎么办。如果有人能给我发一个回复,我会很高兴。

我的class和结构declear

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

我的构造函数:

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

我怀疑导致错误的其他函数。

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

我的主要代码是:

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

错误信息:

C2676 二进制“==”:'student' 未定义此运算符或转换为预定义运算符可接受的类型文件 xutiliy 行 5440

这不是完整的答案,但您可以添加好友功能:

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

这假定 C++20。

如果 C++20 不在范围内尝试:

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

或根据您对 student 之间相等的概念进行调整。它可能 公正

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