C ++使用自己的类继承具有元素的堆栈

时间:2013-12-09 07:07:38

标签: c++ templates inheritance stack

我正在尝试创建自己的类(NodeWithMin)作为C ++中的堆栈元素,并创建一个继承它的新类(StackWithMin)。我想我可以创建新的堆栈类,但是有一些问题初始化新类的新实例并使用它。有没有人对它有好主意?我在一个文件中写了所有类和main。感谢。

#include <stack>

class NodeWithMin{
public:
    int value;
    int min;
    NodeWithMin(int v, int min){
        this->value = v;
        this->min = min;
    }
};

template<class NodeWithMin>
class StackWithMin : stack<NodeWithMin>{
public:
    typedef stack<NodeWithMin> super;
    void push(int value){
        int newMin = min(value, this->min());
        super::push(new NodeWithMin(value, newMin));
    };

    int min(){
        if(this->isEmpty()){
            return numeric_limits<int>::max();
        }else{
            super::peek().min;
        }
    };
};

int main(int argc, const char * argv[])
{
    StackWithMin<class NodeWithMin>* ss;
    ss = new StackWithMin<class NodeWithMin>();
}

2 个答案:

答案 0 :(得分:2)

首先,我删除了

using namespace std;

因此,我可以限定std以消除歧义。

我注意到的第一个问题是这一行:

int newMin = min(value, this->min());

我猜您是在尝试使用min中的algorithm(因为stack不包含min函数):

#include <algorithm>
// snip
int newMin = std::min(value, this->min())

第二个问题是你没有stack<NodeWithMin>的实例,只有一个typedef。因此你需要像这样使用它:

        typedef std::stack<NodeWithMin> super;
        super super_instance;
        void push(int value){
                int newMin = std::min(value, this->min());
                // Why are you using new? It would make
                // It more difficult to avoid memory leaks
                super_instance.push({value, newMin});
        };

第三个问题是stack没有名为isEmpty的成员函数,你的班级也没有。 stack也没有peek成员函数。

        int min(){
                if(super_instance.empty()){
                        return std::numeric_limits<int>::max();
                }else{
                        return super_instance.top().min;
                }
        };

现在它将编译:

int main(int argc, const char * argv[])
{
    StackWithMin<class NodeWithMin>* ss;
    ss = new StackWithMin<class NodeWithMin>();
    ss->push(42);
    delete ss;
}

我没有费心检查逻辑错误。

答案 1 :(得分:1)

#include <stack>
#include <limits>
using namespace std;
class NodeWithMin{
    public:
            int value;
            int min;
            NodeWithMin(int v, int min){
                    this->value = v;
                    this->min = min;
            }
};

template<class NodeWithMin>
class StackWithMin : stack<NodeWithMin>{
    public:
            typedef stack<NodeWithMin> super;
            void push(int value){
                    int newMin = min(value, this->min());
                    super::push(new NodeWithMin(value, newMin));
            };

            int min(){
                    if(this->isEmpty()){
                            return numeric_limits<int>::max();
                    }else{
                            super::peek().min;
                    }
            };
};

int main(int argc, const char * argv[])
{
    StackWithMin<class NodeWithMin>* ss;
    ss = new StackWithMin<class NodeWithMin>();
}

请检查此代码段,您错过了using namespace std,并且必须包含#include <limits>才能使用numeric_limits<int>::max();功能。