No matching function call

Hey guys,

I am trying to implement a hash map I am using the chanining method for collisons ie all buckets point to a linked list of key value pairs,

anyway this implementation isn't the best but it's my first time implementing it, and that will be for a later question,

but for now I am getting an error that I just can't figure out

 Hashing\main.cpp|85|error: no matching function for call to 'List<Pair<std::basic_string<char> > >::addNode(std::basic_string<char>&)'| 


it says there is no matching function call BUT shouldn't the compiler handle this through templating and create an overloading function of this type, I've checked my code but fail to see why the compiler is complaning

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#include <iostream>

using namespace std;

int compression(int hash,int size){

   if(size % 2 == 0)
    size++;

    return hash % size;
}

int hashCode(string str,int bucketSize){

   int g = 31;
   int hash = 0;

   for(int i = 0; i < str.size(); ++i)
      hash = hash * g + str.at(i);

   return compression(hash,bucketSize);
}

template <class T>
struct Pair{

   string key;
   T element;
};

template <class T>
class Node{

  public:
      Node* next;
      Pair<T> pair;

      Node(Node* next,Pair<T> pair): next(next),pair(pair){}
};

template <class T>
class List{

   public:
       Node<T>* head;

       List(){ head = NULL;}

       void addNode(T element){

           Node<T>* newNode = new Node<T>(head,element);
           head = newNode;
       }

       Node<T>* search(string key){

          Node<T>* current = head;

          while(current != NULL){

            if(current->pair->key == key)
                return current;

            current = current->next;
          }
         return NULL;
       }

       bool isEmpty(){ return (head == NULL);}
};

template <class T>
class HashTable{

public:

    static const int size = 100;
    List<Pair<T>> arr[size];

    HashTable(){ }

    void add(T element){

       const int compressionMap = hashCode(element,size);
       arr[compressionMap].addNode(element); // problem here
    }

    T find(string key){

        const int compressionMap = hashCode(key,size);

        if(arr[compressionMap].isEmpty())
            return NULL;

        return arr[compressionMap].search(key);
    }

    //HashTable(){}
};


int main()
{

   string strs[] = {"one","two","three","four","five","six","seven"};

   HashTable<string> ht;

   for(int i = 0; i < 6; ++i)
    ht.add(strs[i])
}

Last edited on
Take a good long look at the declaration of arr.

List<Pair<T>> arr[size];

It is for a Pair.

element is not a Pair.

also, I'd suggest, in main and elsewhere, modern loops

 
for( auto & s : strs ) ht.add( s );


Note, too, that there is a missing ';' on line 111.

Ah yes I see it now

also very true about the loops I've been studying C++ for 3 - 4 years on and off and I've never really learned modern C++ most tuts and books I've followed used C++98

thanks

Ah, then I highly recommend you upgrade your books.

Modern C++ (11 forward) is far superior on many levels, and moving to something this side of the 21st century is going to avoid wasting time and effort. A lot has happened to templates, the standard library, and C++20 is now gaining support among the current compilers - which offers yet another significant new level to templates (concepts).

I'd go so far as to say that if you delay, even a week, moving into more modern books, you are holding yourself back, especially if you view your approach as casual.

Stroustrup, lecturing on that particular point, has said that older C++ required too much "expert" effort, while the newer C++ features opens C++ up to less formal, more casual users (programmers). His observation is that while many are serious programmers (focusing on development as a primary occupation), the language has been opened for other professionals who merely need to write something for that field (say, a mechanical engineer who needs to control an engine).

So, in particular if your thinking is that the older C++ is better for curiosity and casual programming, you've missed the point of modern C++. The truth is the exact opposite. A lot of work went into making the work easier, more reliable, safer, less prone to bugs, with much greater power and much less writing.

Seriously, leave the 90's behind. Especially if you're "dabbling" - just experimenting, just playing. Modern C++ was intended for that (and a lot otherwise - plenty in modern C++ for the dedicated developers, too).
Last edited on
Very good points,

but wouldn't you agree that is also important to know C++98 as many older applications and older systems are implemented in legacy c++, If I was to ever get a role in a company that needed to debug or maintain older systems it would be almost essential to know right?

plus C++ has evolved so I would imagine it would be easier to transition to newer versions of the language rather than vice versa.
I would imagine it would be easier to transition to newer versions of the language rather than vice versa.
I find it easier to understand older versions of C++ when I have a grasp of newer, C++11 and later language features. Believing that learning older versions first makes it easier to learn newer C++ is as much a mistake as thinking to learn C++ one must learn C first. One of the 5 major myths of C++.

https://isocpp.org/blog/2014/12/myths-1

AFTER someone has learned newer C++ going back reviewing earlier versions becomes easier and less likely to promote easy-to-avoid errors.

The tutorial here at CPlusPlus is good if a bit outdated. It, and the reference section, covers C++11, with a bit of C++14.

The Learn CPP tutorial is very good at keeping up to date. You would do yourself a favor to give it a look.

https://www.learncpp.com/

And if you want as up-to-date a reference for C++ as one can get online there is cppreference. Stinks at trying to learn from, but is THE source for non-beginners of what C++ (and C) has to offer.

https://en.cppreference.com/w/

I spend considerable time browsing around cppreference and marveling at what C++11 and later has to offer.

The standards committee keeps enhancing C++, take advantage of what they've created.

Stop trying to create a mnemonic memory circuit using stone knives and bear skins.
Topic archived. No new replies allowed.