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

标签 c++

好的,这是我的问题。有人告诉我,我的类(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/

相关文章:

c++ - OpenGL和GLM矩阵无法正确缩放,始终会缩小

c++ - 通过 winsock 发送 Direct3D 顶点信息

c++ - 当条件为真时,有什么方法可以连接宏参数吗?

C++ win32 api双缓冲示例帮助请

c++ - Android NDK 中的未知类型名称错误

c++ - 需要洗牌并打印

c++ - 如何让月份停止显示 1.00、2.00 等

c++ - 如何分配和更改 QString 的值?

C++ 命名空间和链接不起作用

c++ - 无法编译 libcurl 的静态库