0

I want to implement Hash table in c++.I for some reason get error C2563 Missmatch in formal parameter list in class ChainedHashTable.

Here code for ChainedHashTable class:

#include"LancanaLista.h"
#include"HashTable.h"
template<class T, class R>
class ChainedHashTable : public HashTable<T, R>
{
protected:
    LancanaLista<HashObject<T, R>>* niz;
public:
    ChainedHashTable(unsigned int len)
    {
        this->lenght = len;
        count = 0;
        niz = new LancanaLista<HashObject<T, R>>[len];
    }
    ~ChainedHashTable()
    {
        for (unsigned int i = 0; i < this->length; i++)
        {
            while (!niz[i].isEmpty())
            {
                niz[i].DeleteFromHead();
            }
        }
        delete[] niz;
    }
 
    void insert(HashObject<T, R> obj)
    {
        niz[h(obj)].AddToHead(obj);
        count++;
    }
    void withdraw(HashObject<T, R> obj)
    {
        niz[h(obj)].DeleteEl(obj);
        count--;
    }
    void withdraw(T key)
    {
        HashObject<T, R> obj = find(key);
        withdraw(obj);
    }
    HashObject<T, R> find(T key)
    {
        HashObject<T, R> obj;
        unsigned int i = f(key) % this->lenght;
 
        obj = niz[i].GetHeadEl();
        while (!(obj.isEqualKey(key)))
        {
            obj = niz[i].GetNextEl(obj);
        }
        return obj;
    }
    /*void print()
    {
        for (int i = 0; i < count; i++)
        {
            niz[i].PrintAll();
            cout << endl;
        }
    }*/
 
};

HashTable is base class, ChainedHashTable is derived class
Here code for HashTable class:
#pragma once
#include"HashObject.h"
 
template <class T, class R>
class HashTable
{
protected:
    unsigned int lenght; // velicina tablice
    unsigned int count; // broj elemenata u tablici
 
protected:
    unsigned int h(HashObject<T, R> obj)
    {
        return (f(obj.getKey()) % lenght);
    }
    
    virtual unsigned int f(int i) { return abs(i); }
    virtual unsigned int f(double d)
    {
        if (d == 0)return 0;
        else
        {
            int exp;
            double mantisa = frexp(d, &exp);
 
            return (unsigned int)((2 * fabs(mantisa) - 1) * ~0U);
        }
    }
    virtual unsigned int f(char* s)
    {
        unsigned int res = 0;
        unsigned int a = 7;
        for (int i = 0; s[i] != 0; i++)
        {
            res = res << a ^ s[i];
        }
        return res;
    }
    virtual unsigned int g(unsigned int i)
    {
        return (i + 1) % lenght;
    }
 
public:
    unsigned int GetLenght() { return lenght; }
    virtual double getLoadFactor()
    {
        return (double)count / (double)lenght;
    }
    
};

Here code for HashObject class:
#pragma once
#include<iostream>
using namespace std;
template<class T, class R>
class HashObject
{
protected:
    T key;
    R* record;
 
public:
    HashObject() { key = (T)0; record = nullptr; }
    HashObject(T k) { key = k; record = nullptr; }
    HashObject(T k, R* o) { key = k; record = o; }
    ~HashObject() { deleteRecord(); }
 
    HashObject& operator=(HashObject const& obj)
    {
        if (obj != this)
        {
            key = obj.key;
            record = obj.record;
            
        }
        return *this;
    }
    bool operator ==(HashObject const& obj)
    {
        return record == obj.record;
    }
    void deleteRecord() {
        if (record)
        {
            delete record;
            record = nullptr;
        }
    }
    T getKey() { return key; }
    R* GetRecord() { return record; }
    bool isEqualKey(T k) { return key == k; }
    void print() { cout << key << "|" << record; }
};

Its implemented with LinkedList
Please help me to solve this problem.

  • 2
    Please cut this down to a [mre], with emphasis on the word minimal. When you do that, you'll probably find the problem anyway. – Paul Sanders Apr 24 '22 at 16:36
  • 1
    Possibly an MSVC bug? (clang-cl compiles your code). But, as a fix, in `insert`, change `count++;` to `this->count++;` and, in `withdraw` use `this->count--;`. Related (duplicate?): [Why do I have to access template base class members through the this pointer?](https://stackoverflow.com/q/4643074/10871073) – Adrian Mole Apr 24 '22 at 16:48
  • 1
    Also related: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/10871073) Take out those lines and the error goes away (but others appear). *Probably* because MSVC uses a `count` in its implementation of one of the base STL classes you use. – Adrian Mole Apr 24 '22 at 16:56
  • 1
    Note also, in your constructor, similarly change `count = 0;` to `this->count = 0;`. – Adrian Mole Apr 24 '22 at 17:03

0 Answers0