2011-09-30 144 views
1

我在使用Xcode IDE進行C++編程時是全新的。我所做的是我自己創建了一個Hashtable,並單擊了構建按鈕,因此我得到了編譯錯誤。有沒有人有什麼錯誤的想法?構建錯誤:體系結構x86_64的未定義符號:

這是錯誤:

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) 

哈希表的cpp文件是這樣的:

#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; 
} 

回答

2

模板定義CPP文件裏?嘗試把它放在頭文件中。當您聲明實例時,編譯器將創建該類的一個新實例(MyHashTable < int,int > myVar;),因此在include期間需要存在整個類定義。因此,h文件而不是cpp文件。

我不確定這是否是整個問題,因爲我不使用XCode,但這聽起來像是我的問題。

+0

它的工作,非常感謝! – faz

相關問題