错误:推送函数必须返回一个值。总的来说,任务是创建一个基于数组的容器类,并根据堆栈原理进行访问。F-I push 向数组添加一个新元素,在此之前检查是否还有剩余空间。
#include <iostream>
class Stack {
int* m_head;
int m_size;
int m_index;
public:
Stack(int size) : m_size{ size }, m_index{ 0 } {
m_head = new int[size];
}
~Stack() {
delete[] m_head;
}
bool push (int x){
if (m_index < m_size) {
m_head[m_index++] = x;
return true;
}
std::cout << "the stack is full" << std::endl;
return false;
}
};
int main() {
int n = 5;
Stack stack{n};
for (int i = 0; i < n; i++) {
if (!stack.push(i)) return;
}
}
问题是该函数
main返回int.而你的很简单
return。没有返回值……那是编译器不满意。写点像
顺便说一句,你的错误:推送函数必须返回一个值。- 不对。例如,VC++ 表示
哦
push,如你所见,没有字。GCC 写的一样:
(参见https://ideone.com/BpVJCH)
所以阅读错误消息时要小心......