提问者:小点点

如何在使用带结构的C++链哈希时解决C2676运算符错误?


我得到一个错误,说明错误C2676 binary'==':'student'未定义此运算符或到预定义运算符Project_datastr2可接受类型的转换C:Program Files(x86)Microsoft Visual Studio2019CommunityVCTOolsMSVC14.28.29333IncludeXutility Line:5440

当我点击错误链接时,它会打开一个关于操作符的描述。但我不知道我该怎么办。如果有人能给我寄一封感谢信,我会很高兴的。

我的类和结构声明

#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 binary'==':'student'未定义此运算符或到预定义运算符文件xutily第5440行可接受类型的转换


共1个答案

匿名用户

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

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

或者根据

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

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|结构|c++|链|哈希|解决|c2676|运算符)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?