Question

I am completely new in programming with c++ using the Xcode IDE. What I do is I created a Hashtable myself, and clicked the build button, and I got compilation errors as such. Does anyone have ideas about what is going wrong?

This is the error:

    Undefined symbols for architecture x86_64:
  "MyHashtable<std::string, int>::MyHashtable(int)", referenced from:
      _main in main.o
  "MyHashtable<std::string, int>::Insert(std::string, int)", referenced from:
      _main in main.o
  "MyHashtable<std::string, int>::GetKeys()", referenced from:
      _main in main.o
  "MyHashtable<std::string, int>::GetLength()", referenced from:
      _main in main.o
  "MyHashtable<std::string, int>::GetValue(std::string)", referenced from:
      _main in main.o
  "MyHashtable<std::string, int>::~MyHashtable()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The cpp file of hashtable is as such:

#include <iostream>
#include "Hashtable.h"
#include <string>

template<typename T1,typename T2>
MyHashtable<T1,T2>::MyHashtable(int iSlots) 
{
    if(iSlots<5) iSlots = 5;
    m_hashSlots = new HashElement<T1,T2>*[iSlots];
    m_hashSlots = iSlots;
    m_Length = 0;
}

template<typename T1,typename T2>
MyHashtable<T1,T2>::~MyHashtable()
{
    if(m_hashSlots)
    {
        HashElement<T1, T2> * phead;
        for(int i = 0;i<m_NumSlots;i++)
        {
            phead = m_hashSlots[i];
            while(phead!=0)
            {
                HashElement<T1, T2>* pNext = phead->next;
                delete phead;
                phead = pNext;
            }
        }
        delete m_hashSlots;
        m_hashSlots = 0;
    }
}

template<typename T1,typename T2>
int MyHashtable<T1,T2>::HashKey(T1 key)
{
    char* keyString = (char*)key;
    int keyNum = 0;
    for(int i = 0;i<strlen(keyString);i++)
    {
        int ascK = keyString[i];
        keyNum += ascK;
    }
    return keyNum%m_NumSlots;
}

template<typename T1,typename T2>
T1* MyHashtable<T1,T2>::GetKeys()
{
    T1* keys = new T1[m_Length];
    int index = 0;
    for(int i = 0;i<m_NumSlots;i++)
    {
        HashElement<T1,T2> *phead = m_hashSlots[i];
        while(phead!=0)
        {
            keys[index] = phead->key;
            index++;
            phead = phead->next;
        }
    }
    return keys;
}

template<typename T1,typename T2>
T2 MyHashtable<T1,T2>::GetValue(T1 key)
{
    int index = HashKey(key);
    HashElement<T1,T2> *phead = m_hashSlots[index];
    while(phead)
    {
        if(phead->key==key)
            return phead->value;
        else
            phead = phead->next;
    }
    return NULL;
}


template<typename T1,typename T2>
int MyHashtable<T1,T2>::GetLength()
{
    return m_Length;
}

template<typename T1,typename T2>
bool MyHashtable<T1,T2>::Insert(T1 key, T2 value)
{
    int index = HashKey(key);
    HashElement<T1,T2> *phead = m_hashSlots[index];
    while(phead)
    {
        if(phead->key == key) {
            int newValue = (int)(phead->value);
            newValue += int(value);
            return true;
        } else {
            phead = phead->next;
        }
    }
    HashElement<T1,T2>* newNode = new HashElement<T1,T2>();
    newNode->key = key;
    newNode->value = value;
    phead->next = newNode;
    return true;
}
Was it helpful?

Solution

Template definition inside of cpp file? Try putting it inside of the header file. The compiler will create a new instance of the class when you declare the instance (MyHashTable<int,int> myVar;), and so during include the entire class definition needs to be present. Hence, the h file instead of the cpp file.

I'm not sure if this is the entire problem, as I don't use XCode, but it sounds like the issue to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top