我正在使用模板在C++中实现哈希表和链接列表(没有STL - 不要问),而且我遇到了将它们与g ++链接起来的问题。如果我把所有的.cpp文件包括在一起,一切都可以正常工作,所以我的代码肯定有效,这只是让我绊倒的链接。使用g ++链接模板
我读the bit in the GCC manual about template instantiation,但不知道如何应用它。
我的问题: 我有我的哈希表HashMap<T>
和HashEntry<T>
(<T>
是价值 - 我的钥匙是std::string
S)。我的链接列表有LinkedList<T>
和Node<T>
(其中<T>
是值)。
在我的哈希表,我有:
template <class T> class HashMap {
...
private:
LinkedList< HashEntry<T> >** buckets;
}
这给了我的HashEntry<T>
个链表。
在一个单独的文件,我有我的链表类的声明:
template <class T>
class Node {
...
private:
T data;
}
template <class T> class LinkedList {
...
private:
Node<T> * first;
}
然后,当我尝试(与g++ -c -frepo *.cpp
编译后)链接的一切,我得到:
g++ -frepo -o app LinkedList.o HashMap.o
[...unrelated errors about not having a main method - they go away when I link that in]
HashMap.o: In function `HashMap<int>::~HashMap()':
HashMap.cpp:(.text._ZN7HashMapIiED1Ev[HashMap<int>::~HashMap()]+0x65): undefined reference to `LinkedList<HashEntry<int> >::~LinkedList()'
HashMap.o: In function `HashMap<int>::insert(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
HashMap.cpp:(.text._ZN7HashMapIiE6insertESsi[HashMap<int>::insert(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)]+0xff): undefined reference to `Node<HashEntry<int> >::setData(HashEntry<int>)'
谷歌搜索在我看来,我的程序使用了显式模板类型的建议。该方法适用于HashMap
和HashEntry
(我加(template class HashMap<int>
和template class HashEntry<int>
。
然而,我无法弄清楚如何使这项工作为LinkedList
和Node
类,因为模板实例是HashEntries<int>
,但是,我可以't把它放到LinkedList.h
文件中,因为它是我的哈希表的#include
d。我也无法得到一个高级/ extern声明。
我敢肯定,有一些相当简单的东西我失踪使所有这些工作。任何提示?
是否有你的`LinkedList`析构函数的定义在任何地方?或`节点<...> :: setData()`? – robert 2010-12-22 00:04:20
也许不相关,但是有什么理由不使用make,waf,SCons等。 – robert 2010-12-22 00:05:21