我的驱动程序正在调用带有删除函数的查找函数,如下所示:
remove(find(p, string("Hog")));
我的查找功能是:
Node <T> * find(Node <T> * & pHead, T & e)
我的删除函数是:
Node <T> * remove(const Node <T> * pRemove)
错误表明之间没有匹配的函数调用:
find(Node >*&, std::__cxx11::string) (what the driver is calling)
和
find(Node*&, T&) [with T = std::__cxx11::basic_string] (what my find function is using)
我能看到的唯一区别是驱动程序使用的字符串数据:std::__cxx11::string
我的只有 std::__cxx11::basic_string<char>
.
我不知道这两个有什么区别。关于如何获得此函数调用匹配有什么想法吗?
最佳答案
您无法将临时对象绑定(bind)到引用,因为通常临时对象会立即消失并且对它的引用会悬空。但在通话中它会一直存在直到返回。您可以通过将函数更改为:
来解决问题Node <T> * find(Node <T> * & pHead, const T & e)
^^^^^
const &
延长临时变量的生命周期,直到函数返回。
替代解决方案是不使用临时的:
string s("Hog")
remove(find(p, s));
关于c++ - 两种类型的字符串没有匹配的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58575133/