簡體   English   中英

在派生類中重寫C ++模板函數

[英]Overriding C++ template function in derived class

我試圖寫一個返回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;
    }
};

您需要更改此:

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

至:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM