简体   繁体   中英

Overriding C++ template function in derived class

I am trying to write a stack that returns the minimum element of the stack in O(1), for that I am using a derived class but not getting successful. I am getting an error when trying to call the base class's function from derived class. Would appreciate if you can review the code and provide any input on how to fix it. SCREENSHOT OF ERROR : 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;
    }
};

You need change this:

stack<nodeWithMin>::push(new nodeWithMin(dat, newMin));

to:

stack<nodeWithMin>::push(nodeWithMin(dat, newMin));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM