Tablica z haszowaniem dla klasy

0

Witam

Próbuje stworzyć hasz tablice dla obiektów prostej klasy niestety przy kompilacji takiego kodu.

#include<iostream>
#include<string>
using namespace std;

class K {
   public:
      int n;
      K(int n): n(5) {}
};

class hTab {
      string * a;
      K * k;
      int r;
   public:
      hTab(int n): a(new string[n]), k(new K[n]), r(0) {}
      ~hTab() { delete [] k; delete [] a; }
      int getId(string s) {
         int i = 0;
         while (i != r && s != a[i]) {++i;}
         return i;
      }
      K & operator [] (string s) {
         a[r] = s;
         ++r;
         return k[r-1];
      }
      const K & operator [] (string s) const {return k[getId(s)];}
};

int main() {
   hTab tab(20);
   K a(5);
   tab["sth"] = a;
   cout << tab["sth"].n;
   return 0;
}

Wywali mi takie oto błędy
In constructor 'hTab::hTab(int)'
|16|error: no matching function for call to 'K::K()'
In member function 'const K& hTab::operator const':|
|28|error: passing 'const hTab' as 'this' argument of 'int hTab::getId(std::string)' discards qualifiers [-fpermissive]|

Niestety nie mogę rozgryźć co zrobiłem źle. Będę wdzięczny za każdą pomoc.

Pozdrawiam dagi12

1

Brakuje domyślnego konstruktora w klasie K.

0

Jak wyżej, albo zamienić na ten:

K(int n=5):n(n) {}

1 użytkowników online, w tym zalogowanych: 0, gości: 1