【发布时间】:2010-12-22 00:00:22
【问题描述】:
我正在使用模板在 C++ 中实现哈希表和链表(没有 STL - 不要问),并且在将它们与 g++ 链接时遇到了问题。如果我 #include 我所有的 .cpp 文件一起,一切正常,所以我的代码肯定有效,只是链接让我绊倒。
我阅读了the bit in the GCC manual about template instantiation,但不知道如何应用它。
我的问题:
我的哈希表有 HashMap<T> 和 HashEntry<T>(<T> 是值 - 我的键是 std::strings)。我的链接列表有LinkedList<T> 和Node<T>(其中<T> 是值)。
在我的哈希图中,我有:
template <class T> class HashMap {
...
private:
LinkedList< HashEntry<T> >** buckets;
}
这给了我一个HashEntry<T>s 的链接列表。
在一个单独的文件中,我有我的 Linked List 类声明:
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>。但是,我不能把它放到LinkedList.h 文件中,因为我的哈希表是#included。我也无法获得为其工作的高级/外部声明。
我确信我缺少一些相当简单的东西来完成所有这些工作。有什么建议吗?
【问题讨论】:
-
你的
LinkedList析构函数在任何地方都有定义吗?还是Node<...>::setData()? -
可能不相关,但有什么理由你不使用 make、waf、SCons 等?
-
@kotlinski:正确,我的 HashMap 使用 LinkedList(用于链接),我的文件顶部有一个
#include "LinkedList.h"。 -
@robert:我的 ~LinkedList() 和 setData 在 .cpp 文件 (
template <class T> LinkedList<T>::~LinkedList()) 中。同样,如果我 #include 所有我的 .cpp 文件一起编译。没有构建系统 - 现在只是试图让它从头开始工作。