构建错误:架构x86_64的未定义符号:

时间:2011-09-30 03:01:51

标签: c++ macos xcode4 build-error

我是使用Xcode IDE进行c ++编程的全新工具。我做的是我自己创建了一个Hashtable,然后单击了构建按钮,我得到了编译错误。有没有人对出了什么问题有所了解?

这是错误:

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

1 个答案:

答案 0 :(得分:2)

cpp文件里面的模板定义?尝试将其放在头文件中。当您声明实例(MyHashTable&lt; int,int&gt; myVar;)时,编译器将创建该类的新实例,因此在包含期间需要存在整个类定义。因此,h文件而不是cpp文件。

我不确定这是否是整个问题,因为我不使用XCode,但这对我来说听起来像是问题。