C++内部类 ctor 和参数
C++ Inner Class Ctor and Parameters
我在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
.
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 为什么在 ctor 的参数列表中将成员"x"的类型替换为"decltype(x)"会破坏类模板参数推导?
- 模板 ctor 类型推导不起作用(没有匹配的构造函数用于初始化 ...)与函数<>参数
- 委派的 ctor 是否受参数计算顺序的影响?
- 如果每个参数都可以转换为特定类型,则启用 ctor
- C++内部类 ctor 和参数
- 通过ctor-rvale参数进行Lambda赋值
- 当使用类型为"类&&"的变量作为参数时,为什么调用复制 ctor 而不是移动 ctor
- c++友元类作为ctor的参数
- 插入>>运算符重载:从 CIN 检索对象的 CTOR 参数时的异常处理
- 根据模板参数启用模板 Ctor
- C++使用 ctor 参数在类中实例化类
- 在Ctor调用中,将std::initializer_list与括号内的参数分离
- std::array 作为字段的语法是什么,里面的对象必须使用接受参数的 ctor 构造
- 同时在另一个类中创建类的对象,该类仅具有参数化的 Ctor "error: x is not a type"
- c++ 11: bind std::sort用于用户定义的类型,然后使用该绑定作为ctor的参数
- 为什么"std::p air"将"std::tuple"作为ctor参数类型而不是"const std::tuple&"?
- 为什么分配器类型作为模板参数而不是 ctor 的参数?
- 动态分配数组时设置Ctor参数