const_iterator cbegin() const { return const_iterator(front_,this); } const_iterator cend() const{ return const_iterator(nullptr,this); } 我对此错误消息进行了一些研究,但是没有答案可以帮助我了解问题所在。我不知道为什么会给我这个错误,希望能得到帮助和澄清,谢谢。

问题描述 投票:0回答:1

const_iterator(Node* curr, DList* theList)

接受一个非const DList *,但它被称为

c++ type-conversion const
1个回答
0
投票
this方法中的constconst,因此此处thisconst DList *,不能用作期望非const DList *的参数的参数。
简单的解决方法是替换

const_iterator(Node* curr, DList* theList) { curr_ = curr; myList_ = theList; }

const_iterator(Node* curr, const DList* theList): curr_(curr), myList_ (theList) // using member initializer list for reasons of taste { }

并且如果可能,请始终遵循const正确性。例如,成员

DList* myList_;

必须成为

const DList* myList_;

或您刚刚移动了错误消息。问题中当前尚未完全实现的其他成员函数可能需要修改。

在我看来,这应该是可能的。
© www.soinside.com 2019 - 2024. All rights reserved.