返回语句打破我的功能C
Return statement breaking my function c++
,所以我在main中调用一个函数: B.remove()
来这里 m_arrays[m_oldest]->remove()
然后最后在这里:
int MyClass::remove() {
cout << "this is the remove function"<< endl; //debugging line
int lastVal = 0;
for (int i = start; i < capacity; i++) { //actual remove function
if (m_array[i] != NULL) {
int lastVal = m_array[i];
m_array[i] = NULL; // sets to null
m_size--; //increment
break;
}
}
m_start++; //increment
return lastVal;
}
如果我删除包含LASTVAL的代码,则该函数正常工作。为什么是这样?有什么简单的解决方法吗?如您所见,我试图在从数组中删除值之前返回值。我知道我应该使用向量或其他标准方法,但我不能。我在教科书中看了看,但找不到想要的东西。任何帮助都非常感谢。
您的代码中有两个int lastVal
语句 - 一个在for
循环之前,其中一个是一个。尝试删除for
循环内的声明(并将其设置为分配lastVal = m_array[i];
),看看它是否有效。
相关文章:
- 询问在设计我的手臂模拟器功能表示格式1
- 反向功能超出了我的 cpp 程序的范围
- 为什么我的功能在使用 goto 时会给我带来"expected primary-expression before '}' token"?
- 为什么我的打印功能不起作用?链表
- 为什么我的显示功能会终止程序?
- "configure"脚本如何检测我的系统是否支持某些头文件或 cpp 功能?
- 如何使我的 cmake 项目所需的某些 c++ 功能
- 为什么我的删除功能总是出现分段错误
- 如何改进我的"String"对象比较功能
- 使用链表.为什么我的插入功能不起作用?
- 我的显示功能有问题
- 为什么我的打印功能之一删除节点
- 为什么我的删除功能中存在错误
- 如果我使用此功能,我的控制台崩溃
- 如何将此功能连接到我的连接按钮?
- 我的模板功能具有通用引用不起作用
- 我的班级功能的分段故障
- 我的朋友功能无法访问私人变量
- C //尝试重写一个工作代码以包括我的功能
- 尽管我的功能和cout语句的放置错误,但我的程序为什么会起作用