Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用g+链接模板+; 我用C++实现了一个哈希表和链表(没有STL -不要问),我遇到的问题是把它们与G++连接起来。如果我把所有的.cpp文件放在一起,一切都会正常工作,所以我的代码肯定会正常工作,只是链接把我绊倒了_C++_Templates_Linker_G++ - Fatal编程技术网

用g+链接模板+; 我用C++实现了一个哈希表和链表(没有STL -不要问),我遇到的问题是把它们与G++连接起来。如果我把所有的.cpp文件放在一起,一切都会正常工作,所以我的代码肯定会正常工作,只是链接把我绊倒了

用g+链接模板+; 我用C++实现了一个哈希表和链表(没有STL -不要问),我遇到的问题是把它们与G++连接起来。如果我把所有的.cpp文件放在一起,一切都会正常工作,所以我的代码肯定会正常工作,只是链接把我绊倒了,c++,templates,linker,g++,C++,Templates,Linker,G++,我读了,但不知道如何应用它 我的问题是: 我的哈希表有一个HashMap和HashEntry(是值-我的键是std::strings)。我的链接列表有链接列表和节点(其中是值) 在我的哈希映射中,我有: template <class T> class HashMap { ... private: LinkedList< HashEntry<T> >** buckets; } 通过谷歌搜索,我看到了一些关于声明我的程序使用的

我读了,但不知道如何应用它

我的问题是: 我的哈希表有一个
HashMap
HashEntry
是值-我的键是
std::string
s)。我的链接列表有
链接列表
节点
(其中
是值)

在我的哈希映射中,我有:

template <class T> class HashMap {
    ...
    private:
        LinkedList< HashEntry<T> >** buckets;
 }
通过谷歌搜索,我看到了一些关于声明我的程序使用的显式模板类型的建议。这适用于
HashMap
HashEntry
(我添加了(
模板类HashMap
模板类HashEntry

但是,我不知道如何使
LinkedList
Node
类可以这样做,因为模板实例是
HashEntries
的。但是,我不能将其放入
LinkedList.h
文件中,因为我的哈希表中包含了
d。我也无法获得高级/外部声明为了它


我确信我缺少了一些非常简单的东西来完成所有这些工作。有什么提示吗?

如果您正在创建模板类,那么将它们放在.cpp文件中并单独编译不是一个好主意。正确的方法是将它们放在.h文件(声明和定义)中,并在需要的地方包含它们

原因是除非定义了模板参数,否则实际上不会编译模板


(有意避免提及导出关键字。)

看起来您正在LinkedList.cpp中定义模板类成员。模板通常需要完全定义(而不仅仅是声明)在.h文件中。有关解决此问题的方法,请参阅,但我会避免它--它会导致更多问题。

是否有
LinkedList
destructor anywhere?或
Node::setData()的定义
?可能不相关,但有什么原因你不使用make、waf、SCons等吗?@kotlinski:正确,我的HashMap使用LinkedList(用于链接),我在文件顶部有一个
#include“LinkedList.h”
。@robert:我在.cpp文件中有我的~LinkedList()和setData(
模板LinkedList::~LinkedList()
)再次强调,如果我把所有的.cpp文件都包含在一起,它就会编译。没有构建系统-现在只是想让它从头开始工作。你知道,在C++0x中有
导出
的东西被删除了…哦,对了:P+1谢谢-我想我会让它工作。现在我只是把.cpp文件包含在.h文件中。这样可以吗是吗,还是我真的应该在类声明中移动代码?@Taj Morton将其移动到类声明中。包括cpp文件通常被认为是不好的形式。
template <class T>
    class Node {
        ...
        private:
             T data;
}

template <class T> class LinkedList {
     ...
     private:
     Node<T> * first;
}
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>)'