容器列表,在for循环中出现错误
Container list, I get error in for loop
我想知道为什么我不能在这个方法中使用for循环,我在替换和插入方法中使用它,但在听到我在for循环行中得到一些错误。
Contact return(const list<Contact> &listOf, int index) {
for(list<Contact>::iterator it = listOf.begin(); it != listOf.end(); it++){
}
return Contact(); //dummy return. don't know what to do here
}
我很感激一些关于如何实现代码的帮助,我要返回什么?
我的意思是我知道如何检查if语句以获得正确的对象,但我不知道在if语句中我要做什么以及要写什么来代替"Contact();"
为const列表使用const_iterator
Contact return_function(const list<Contact> &listOf, int index) {
for(list<Contact>::const_iterator it = listOf.begin(); it != listOf.end(); it++){
}
return Contact(); //dummy return
}
编辑。
您需要const_iterator
,因为listOf
是const
。
Contact return(const list<Contact> &listOf, int index) {
// ^^^^^---const container-----<
// ^
// const_iterator--vvvvvv because of ->|
for(list<Contact>::const_iterator it = listOf.begin(); it != listOf.end(); it++){
另外,您必须重命名您的函数,return
是一个关键字。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 方法内部但循环仍得到预期的不合格id错误C++
- C++ - 循环访问指针数组会导致错误
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- For 循环 C++ 中的值错误
- 为什么我的 for 循环给出错误:X 没有命名类型
- 类方法 - 数据结构中 For 循环的运行时错误
- 试图避免在 for 循环中出现 if-else 语句,但代码似乎有一些错误
- 如何向用户显示特定错误,要求他/她使用循环再次提供输入?
- 循环 c++ 中的分段错误
- 虽然循环即使应用了错误条件也不会退出
- 错误 C2512 视觉C++(并且不是循环依赖项)
- 使用基于范围的指针循环和向量时出现C++错误
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 在指针迭代中使用for循环时出现分段错误
- 向量循环抛出2个错误
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- C++,查找最低值,函数错误:循环最多运行一次(循环增量从未执行)