C++内部类 ctor 和参数

C++ Inner Class Ctor and Parameters

本文关键字:参数 ctor 内部类 C++      更新时间:2023-10-16

我在List内部为内部类ListIterator创建 ctor 时遇到问题,我不知道为什么这不起作用:

template<class T, int length>
class List {
public:
    typedef struct Row {
        T data;
        int prev;
        int next;
    } Row;
    class ListIterator {
        typedef ListIterator iterator;
        public:
            ListIterator(int mPos, Row mRow) : pos(mPos) , row(mRow){}
            //methods
        private:
            int pos;
            Row row;
    };
    typedef ListIterator iterator;
    iterator begin() const;
private:
    Row storage[length];
    int startDataIdx = 0;
};
template<class T, int length>
typename List<T,length>::iterator
List<T, length>::begin() const{
    iterator item_iterator(startDataIdx, storage[startDataIdx]);
    return(item_iterator);
}

没有匹配函数调用 'List::ListIterator::ListIterator(const int&, const Row&('

我想创建一个与List参数相同的T ListIterator<T>(显然是因为它迭代了与List包含的相同类型的元素(,我该怎么做?在方法中使用new会更好begin()还是保持原样(不要使用新的,无论可能有什么优点(?

ListIterator(int &mPos, Row* mRow) : pos(mPos) , row(mRow){}

迭代器构造函数的第一个参数是对可变int的引用。

List<T, length>::begin() const{
   iterator item_iterator(startDataIdx, ...

您正在const类方法中构造迭代器。 this const在这里。 startDataIdx在这里有效地const,我相信你明白,你不能创建一个对const对象的非const引用。

在这种情况下,使用对迭代器的参数引用绝对不会实现任何有用的操作。只需摆脱引用,并传入一个简单的int.