简体   繁体   English

const_iterator的初始化程序没有匹配的构造函数

[英]No matching constructor for initializer of const_iterator

Alright so here's my problem. 好吧,这是我的问题。 I'm being told that there is no matching constructor for my class. 有人告诉我,我的课程没有匹配的构造函数。 Here's the code that's calling it. 这是调用它的代码。 (ignore the inputs. it doesn't seem to matter what I put in, it all comes out bad). (忽略输入。我输入的内容似乎无关紧要,所有结果都不好)。

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

and here are the initializers. 这是初始化器。

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

for the first one, the compiler says that 2 arguments are expected and only one is provided and the second says that an l-value is expected. 对于第一个,编译器说需要两个参数,并且只提供一个参数,第二个说需要一个l值。

Your compiler is trying to find a copy-constructor for const_iterator that can be used to initialize the return value of end() - the issue is not with the return statement itself, but with the copying of the return expression into the return value. 您的编译器正在尝试为const_iterator查找可用于初始化end()返回值的复制构造函数-问题不在于return语句本身,而是与将return表达式复制到返回值中有关。

Since you're returning a temporary, you need a copy constructor that can take a temporary ( r-value ). 由于您要返回一个临时项,因此需要一个可以使用临时项( r-value )的副本构造函数。 A reference to non-const can't be bound to a temporary, so your second constructor can't be chosen. 不能将对非const的引用绑定到临时对象,因此无法选择第二个构造函数。

On the other hand, a reference to const could match. 另一方面,对const的引用可以匹配。 So since you're not modifying the argument anyway, change your signature: 因此,由于您仍然不修改参数,因此请更改签名:

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM