gpt4 book ai didi

c++ - const_iterator 的初始值设定项没有匹配的构造函数

转载 作者:行者123 更新时间:2023-11-28 00:23:09 25 4
gpt4 key购买 nike

好的,这是我的问题。有人告诉我,我的类(class)没有匹配的构造函数。这是调用它的代码。 (忽略输入。我输入的内容似乎无关紧要,结果都很糟糕)。

const_iterator end() const { return const_iterator(root->parent,true); }

这是初始化程序。

const_iterator(Node *n, bool b):node(n),end(b) {}
const_iterator(const_iterator &that):node(that.node),end(that.end){}

对于第一个,编译器表示需要 2 个参数,但只提供了一个,第二个表示需要一个左值。

最佳答案

您的编译器正在尝试为 const_iterator 寻找可用于初始化 end() 返回值的复制构造函数 - 问题不在于返回语句本身,但将返回表达式复制到返回值中。

由于您要返回一个临时值,因此您需要一个可以采用临时值(r-value)的复制构造函数。对非常量的引用不能绑定(bind)到临时对象,因此不能选择第二个构造函数。

另一方面,对 const 的引用可以匹配。因此,既然您无论如何都没有修改参数,请更改您的签名:

const_iterator(const_iterator const& that):node(that.node),end(that.end){}

const_iterator(const const_iterator& that):node(that.node),end(that.end){}

关于c++ - const_iterator 的初始值设定项没有匹配的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26458561/

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