簡體   English   中英

const_iterator的初始化程序沒有匹配的構造函數

[英]No matching constructor for initializer of const_iterator

好吧,這是我的問題。 有人告訴我,我的課程沒有匹配的構造函數。 這是調用它的代碼。 (忽略輸入。我輸入的內容似乎無關緊要,所有結果都不好)。

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){}

對於第一個,編譯器說需要兩個參數,並且只提供一個參數,第二個說需要一個l值。

您的編譯器正在嘗試為const_iterator查找可用於初始化end()返回值的復制構造函數-問題不在於return語句本身,而是與將return表達式復制到返回值中有關。

由於您要返回一個臨時項,因此需要一個可以使用臨時項( r-value )的副本構造函數。 不能將對非const的引用綁定到臨時對象,因此無法選擇第二個構造函數。

另一方面,對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){}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM