我在Ubuntu上实现AVL树时使用模板。
当我写template class AVLTree<std::list<int> >;
时,该文件将无法编译,它告诉我:
未定义引用 'AVLTree <std::__cxx11::list><std::__cxx11:::basic_string><char,>, std::allocator >, std::allocator <std::__cxx11::basic_string><char,> , std::allocator >>>>::insert(std::__cxx11::basic_string <char,>char_traits , std::allocator >('
而且我不明白它没有参考的内容。
但是当我写template class AVLTree<std::list<string> >;
时,它的编译效果很好
我需要AVLTree
存储存储字符串值的链表。
为什么一个编译而另一个不编译?如何解决我的问题?
PS:我已经包含了<list>
,<string>
和<iostream>
,以及我自己的头文件。
仔细检查错误消息表明链接器找不到AVLTree::insert(string)
方法。
根据您发布的稀疏信息,我最好的假设是您将以下行中的模板参数从 list<string>
更改为 list<int>
:
template class AVLTree<std::list<string>>;
这行代码显式告知编译器使用 list<string>
作为模板参数实例化 AVLTree 模板的版本。因此,当您尝试在更改后编译代码时,它会为您提供错误消息,指出它找不到AVLTree::insert(string)
函数,因为编译器现在正在为list<int>
生成代码。
您的程序包含引用 AVLTree<list<string>>
的其他代码。至少,您还必须更新该代码才能使用list<int>
。
简化为可以在此站点上发布代码的内容,那么您将在此过程中发现问题,或者至少会改变以获得一个好的答案。