2011-09-30 170 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

相关问题