我有一組文件可以用makefile編譯來創建單獨的哈希程序。 該程序的功能,直到我添加代碼插入,刪除,幷包含功能。我直接從書中提取代碼,但是我得到了一個模糊的錯誤,我無法弄清楚,希望有人能夠幫助識別它。因爲我是在一個受過教育的猜測錯誤的原因不會在外部的代碼中找到我沒有張貼整個程序(但我可能是錯的)C++哈希程序中未定義的符號錯誤
有問題的錯誤是:
Undefined first referenced
symbol in file
hash(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)hashApp.o
而且,不知道這是相關的,但如果我嘗試和編譯功能的.cpp文件本身,我得到:
Undefined first referenced
symbol in file
main /opt/csw/gcc3/lib/gcc/sparc-sun-solaris2.8/3.4.6/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
這裏的功能,字符串正在散列列表向量:
template <class HashObj>
bool HashTable<HashObj>::contains(HashObj &item)
{
const list<HashObj> & whichList = theLists[ myhash(item) ];
return find(whichList.begin(), whichList.end(), item) != whichList.end();
}
template <class HashObj>
bool HashTable<HashObj>::insert(const HashObj &item)
{
list<HashObj> & whichList = theLists[ myhash(item) ];
if(find(whichList.begin(), whichList.end(), item) != whichList.end())
return false;
whichList.push_back(item);
return true;
}
template <class HashObj>
bool HashTable<HashObj>::remove(const HashObj &item)
{
list<HashObj> & whichList = theLists[ myhash(item) ];
typename list<HashObj>::iterator itr = find(whichList.begin(), whichList.end(), item);
if(itr == whichList.end())
return false;
whichList.erase(itr);
return true;
}
這是從同一文件的myhash功能:
template <class HashObj>
int HashTable<HashObj>::myhash(const HashObj &item) const
{
int hashVal = hash(item);
hashVal %= theLists.size();
if (hashVal < 0)
hashVal += theLists.size();
return hashVal;
}
上面的.cpp代碼具有一個包括用於hashTable.h,其又包括hashPrototypes.h
在hashPrototypes .h是
int hash(int key);
int hash(const string &key);
我的散列函數從一個makefile中編譯,該makefile根據什麼y創建一個可執行文件ou進入。例如,我使用hash1.cpp,所以通過輸入make HASH = hash1,它應該將它們一起編譯。
這裏是我的hash1.cpp代碼:
#include "hashTable.h"
#include <cmath>
#include <cstdlib>
using namespace std;
template <class HashObj>
int hash(const HashObj &item)
{
int hashVal = 0;
for(int i = 0; i < item.length(); i++)
hashVal = 37 * hashVal + item[ i ];
return hashVal;
}
如果你認爲錯誤是在Makefile,這裏是生成文件代碼:
# Make file for hashing
# Executable for the program will be in: hashTest
#default function is looked for in hashS1
#to give it another function make=filename without the suffix
HASH = hashS1
$(HASH)Test: $(HASH).o hashTable.o hashApp.o
g++ -o $(HASH)Test $(HASH).o hashTable.o hashApp.o
hashApp.o: hashTable.h hashPrototypes.h hashApp.cpp hashTable.cpp
g++ -c hashApp.cpp
hashTable.o: hashTable.h hashTable.cpp $(HASH).cpp
g++ -c hashTable.cpp
$(HASH).o: hashPrototypes.h $(HASH).cpp
g++ -c $(HASH).cpp
clean:
rm -f *.o
touch *
你可以發佈你的'myhash()'函數的代碼嗎?看起來你可能正在使用'std :: hash',在這種情況下,你可能只需要'#include',但是很難確定。 –
'當我嘗試用自己的函數編譯.cpp文件...'不,這是不相關的。 – john
你有一個未定義的符號「哈希」,但是你發佈的代碼中沒有使用稱爲「哈希」的符號。所以從發佈的代碼來看,這有點神祕。發佈更多代碼。 – john