gpt4 book ai didi

c++ - 为什么std::list work but not std::list

转载 作者:行者123 更新时间:2023-12-02 11:14:11 27 4
gpt4 key购买 nike

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

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

undefined reference to `AVLTree < std::__cxx11::list < std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> >, std::allocator < std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> > > > >::insert(std::__cxx11::basic_string < char, std::char_traits < char>, std::allocator < char> >)'



我没有得到它没有提到的内容。

但是当我写 template class AVLTree<std::list<string> >;时它编译就很好

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

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

PS:我包括了 <list><string><iostream>,以及我自己的头文件。

最佳答案

仔细检查该错误消息可知,链接器找不到AVLTree::insert(string)方法。

根据您发布的稀疏信息,我最好的假设是您将以下行中的template参数从list<string>更改为list<int>:

template class AVLTree<std::list<string>>;

此行代码明确告诉编译器使用 list<string>作为模板参数来实例化AVLTree模板的版本。因此,当您尝试在更改后编译代码时,它会显示错误消息,即找不到 AVLTree::insert(string)函数,因为编译器现在正在为 list<int>生成代码。

您的程序包含引用 AVLTree<list<string>>的其他代码。至少您必须更新该代码才能使用 list<int>

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

关于c++ - 为什么std::list<string> work but not std::list<int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47233883/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com