我正在尝试编写一个堆栈,它返回O(1)中堆栈的最小元素,因为我使用派生类但没有成功。我试图从派生类调用基类的函数时遇到错误。如果您可以查看代码并提供有关如何修复它的任何输入,将不胜感激。屏障错误:http://imgur.com/PhoNRpq
#include<iostream>
#include<cstdlib>
#include<stack>
using namespace std;
#define MAX 999999
int findmin(int a, int b)
{
if (a < b)
return a;
return b;
}
class nodeWithMin
{
public:
int val, min;
nodeWithMin(int x, int y)
{
val = x;
min = y;
}
};
class myStack : public stack<nodeWithMin>
{
public:
void push(int dat)
{
int newMin = findmin(dat, stackMin());
stack<nodeWithMin>::push(new nodeWithMin(dat, newMin));
}
int stackMin()
{
if (this->empty())
return MAX;
else
return this->top().min;
}
};
答案 0 :(得分:1)
你需要改变这个:
stack<nodeWithMin>::push(new nodeWithMin(dat, newMin));
为:
stack<nodeWithMin>::push(nodeWithMin(dat, newMin));