1

I have this problem when I am trying to build my project, I have 6 errors of Undefined symbols with my functions:

Showing Recent Issues Undefined symbol: Hashint::deleteHash(int)

Undefined symbol: Hashint::insertHash(int)

Undefined symbol: Hashint::searchHash(int)

Undefined symbol: Hashint::print()

Undefined symbol: HashTable<int, int>::HashTable(int)

Undefined symbol: vtable for Hashint

I have a template class with an inner class (Item):

template <class T, class K>
class HashTable
 
{
public:
    enum state {empty, full, deleted};
    class Item
    {
    public:
        T data;
        K key;
        state flag;
        Item(){}
        Item(T d, K k, state f){ data=d; key=k; flag=f;}
        
    };

    
public:
    Item* table;
    int size;
    
    HashTable(int sizeHash);

    virtual ~HashTable() {}
    
    virtual int h1(K k);
    virtual int h2(K k);
    
    //returns the index of the hashing table for the key k at the try number i
     virtual int hash(K k, int i);
     virtual int searchHash(K k); // searches and return the index
    virtual  int insertHash(K k); // insert
    virtual void deleteHash(K k); // delete
     virtual void print();
};

and I have my class Hashint that inherit from hashtable:

class Hashint : public HashTable<int,int>  {
   
public:
    int sizeTable;
    Item* table;
    
    
    Hashint(int size):HashTable<int, int>(size)
    {
        table= new Item [size];
        sizeTable = size;
        for (int i=0; i<sizeTable; i++)
            table[i].flag = empty;
    }
        
    int h1(int k);
    int h2(int k);
    int hash(int k, int i);
    int searchHash(int k);
    int insertHash(int k);
    void deleteHash(int k); 
    void print();    
};

I have also a cpp file of my Hashtable class but the post will be too long if I post it...

I don't understand what's the problem, I did the inheritance wrong? Or maybe the problem is the template? I would be really happy if you could help me! Thank u!!

Elish
  • 61
  • 5
  • https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file probably – Mat Nov 16 '20 at 21:41
  • Make sure you implemented the `HashTable` template in a header and not a .cpp file. Implementing it in a cpp file will cause this type of error. – drescherjm Nov 16 '20 at 21:49
  • I change it, I put everything in the .h file but it still not working... its displays the same errors with the undefined symbol – Elish Nov 16 '20 at 22:11

1 Answers1

1

The functions inside the template class has to be defined before you instantiate a template class. Add the function definitions to the header file and it should work.

Check this code for example:

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
  {a=first; b=second;}
T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}

int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}

getMax has been defined outside of the class before it is used in the main code

Found this code in this link

CV_Ruddha
  • 406
  • 2
  • 13