为什么std :: list <string>工作但不是std :: list <int>

时间:2017-11-11 02:51:21

标签: c++ compiler-errors compilation undefined-reference

我在Ubuntu上实现AVL树时使用模板。

当我写template class AVLTree<std::list<int> >;时,文件将无法编译,它告诉我:

  

未定义对`AVLTree&lt;的引用std :: __ cxx11 :: list&lt; std :: __ cxx11 :: basic_string&lt; char,std :: char_traits&lt; char&gt ;,std :: allocator&lt;炭&GT; &gt ;,std :: allocator&lt; std :: __ cxx11 :: basic_string&lt; char,std :: char_traits&lt; char&gt ;,std :: allocator&lt;炭&GT; &GT; &GT; &GT; &gt; :: insert(std :: __ cxx11 :: basic_string&lt; char,std :: char_traits&lt; char&gt;,std :: allocator&lt; char&gt;&gt;)'

我没有得到它没有引用的内容。

但是当我写template class AVLTree<std::list<string> >;

时,它编译得很好

我需要让AVLTree存储存储字符串值的链接列表。

为什么要编译而另一个不编译?如何解决我的问题?

PS:我已将<list><string><iostream>以及我自己的头文件包括在内。

1 个答案:

答案 0 :(得分:1)

仔细检查错误消息表明链接器无法找到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>

另外,如果您将问题简化为可以在此网站上发布代码的内容,那么您将在该过程中找到问题,或者至少可以获得更好的答案。