简体   繁体   English

使用g ++链接模板

[英]Linking templates with g++

I'm implementing a hash table and linked list in C++ (no STL - don't ask) using templates, and I'm running into problems linking them with g++. 我正在使用模板在C ++中实现哈希表和链表(没有STL - 不要问),而且我遇到了将它们与g ++链接的问题。 If I #include all my .cpp files together, everything works, so my code definitely works, it's just the linking that's tripping me up. 如果我#include所有我的.cpp文件,一切正常,所以我的代码肯定有效,它只是让我绊倒的链接。

I read the bit in the GCC manual about template instantiation , but wasn't sure how to apply it. 在GCC手册中读到了关于模板实例化的一些内容 ,但不确定如何应用它。

My problem: I have a HashMap<T> and HashEntry<T> for my hash table ( <T> is the value - my keys are std::string s). 我的问题:我的哈希表有一个HashMap<T>HashEntry<T><T>是值 - 我的键是std::string s)。 My linked list has LinkedList<T> and Node<T> (where <T> is the value). 我的链表有LinkedList<T>Node<T> (其中<T>是值)。

In my hash map, I have: 在我的哈希映射中,我有:

template <class T> class HashMap {
    ...
    private:
        LinkedList< HashEntry<T> >** buckets;
 }

Which gives me a linked list of HashEntry<T> s. 这给了我一个HashEntry<T>的链表。

In a separate file, I have my Linked List class declaration: 在一个单独的文件中,我有我的Linked List类声明:

template <class T>
    class Node {
        ...
        private:
             T data;
}

template <class T> class LinkedList {
     ...
     private:
     Node<T> * first;
}

Then, when I try to link everything (after compiling with g++ -c -frepo *.cpp ), I get: 然后,当我尝试链接所有内容时(在使用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>)'

Googling around, I've seen suggestions for declaring the explicit template types that my program uses. 谷歌搜索,我已经看到了声明我的程序使用的显式模板类型的建议。 This works for the HashMap and HashEntry (I added ( template class HashMap< int > and template class HashEntry< int > . 这适用于HashMapHashEntry (我添加了( template class HashMap< int >template class HashEntry< int >

However, I can't figure out how to make this work for LinkedList and Node classes, since the template instances are of HashEntries<int> . 但是,我无法弄清楚如何使LinkedListNode类工作,因为模板实例是HashEntries<int> BUT, I can't put that into the LinkedList.h file, since it's #include d by my Hash tables. 但是,我不能把它放到LinkedList.h文件中,因为它是由我的哈希表#include d。 I also couldn't get an advanced/extern declaration working for it. 我也无法获得高级/外部声明。

I'm sure there's something fairly simple that I'm missing to make all this work. 我确信有一些相当简单的东西让我无法完成所有这些工作。 Any tips? 有小费吗?

If you are making template classes, it is not a good idea to put them in .cpp files and compile separately. 如果您正在制作模板类,那么将它们放在.cpp文件中并单独编译并不是一个好主意。 The right way is to put them in .h files (both declaration and definition) and include them where you need them. 正确的方法是将它们放在.h文件(声明和定义)中,并将它们包含在您需要的地方。

The reason is that templates won't actually be compiled unless their template arguments are defined. 原因是除非定义了模板参数,否则实际上不会编译模板。

(Intentionally avoiding mentioning the export keyword.) (故意避免提及export关键字。)

It looks like you're defining template class members in LinkedList.cpp. 看起来你在LinkedList.cpp中定义了模板类成员。 Templates generally need to be completely defined (not just declared) in the .h file. 模板通常需要在.h文件中完全定义(而不仅仅是声明)。 For ways around this, see storing C++ template functions in a .cpp file. 有关此方法,请参阅在.cpp文件中存储C ++模板函数。 But I would avoid it--it causes more problems than it's worth. 但我会避免它 - 它导致的问题多于它的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM