简体   繁体   中英

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.

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.

Since you're returning a temporary, you need a copy constructor that can take a temporary ( r-value ). A reference to non-const can't be bound to a temporary, so your second constructor can't be chosen.

On the other hand, a reference to const could match. 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){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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