简体   繁体   English

C++中的堆栈实现

[英]Stack implementation in C++

I'm trying to implement a stack class as a learning exercise.我正在尝试实现堆栈类作为学习练习。 This is my StackNode class,这是我的 StackNode 类,

template <typename T>
class StackNode{
public:
    T data_{};
    StackNode<T> next_;

    StackNode() : data_(nullptr), next_(nullptr){}
    StackNode(const T& data_) : data_(data_),next_(nullptr) {}

};

I use this node to create a Stack, this is the code,我用这个节点创建了一个Stack,这是代码,

template <typename T>
class Stack{
private:

    StackNode<T> top_;
    std::size_t size_{};

public:
    Stack() : top_(nullptr), size_ (0){}
    void push(const T& item){
        StackNode<T> p{item};
        p.next_ = top_;
        top_ = p;
    }

    T pop(){
       StackNode<T> p = top_;
        top_ = top_.next_;
        return p;
    }

    T peek(){
        return top_.data_;
    }

};

This is the calling Client,这是调用客户端,

Stack<int> stack{};
stack.push(12);
stack.push(13);
std::cout << stack.peek() << std::endl;
stack.pop();
std::cout << stack.peek() << std::endl;

I get the following compilation errors我收到以下编译错误

In file included from /Users/mstewart/Dropbox/codespace/private/cpp-drive/data-structures/main.cpp:2:
In file included from /Users/mstewart/Dropbox/codespace/private/cpp-drive/data-structures/Stack.hpp:5:
/Users/mstewart/Dropbox/codespace/private/cpp-drive/data-structures/StackNode.h:12:18: error: field has incomplete type 'StackNode<int>'
    StackNode<T> next_;
                 ^
/Users/mstewart/Dropbox/codespace/private/cpp-drive/data-structures/Stack.hpp:13:18: note: in instantiation of template class 'StackNode<int>' requested here
    StackNode<T> top_;
                 ^
/Users/mstewart/Dropbox/codespace/private/cpp-drive/data-structures/main.cpp:6:16: note: in instantiation of template class 'Stack<int>' requested here
    Stack<int> stack{};
               ^
/Users/mstewart/Dropbox/codespace/private/cpp-drive/data-structures/StackNode.h:9:7: note: definition of 'StackNode<int>' is not complete until the closing '}'
class StackNode{
      ^

Can someone help me understand what I'm doing wrong.有人可以帮助我了解我做错了什么。 I'm new to C++我是 C++ 新手

"A class instance cannot contain an instance of itself" - Oliver Charlesworth, The Comment Section. “类实例不能包含自身的实例” - Oliver Charlesworth,评论部分。

As mentioned elsewhere , the class is not fully defined at the point of use (see the last line in the compiler error message), and would have problems with memory layout. 正如别处提到的,类在使用时没有完全定义(参见编译器错误消息中的最后一行),并且会出现内存布局问题。

You therefore need to use pointers to manage the items in the array.因此,您需要使用指针来管理数组中的项目。 One relatively straightforward adjustment to your code is to use std::unique_ptr instead (avoiding manual memory management with new and delete ).对代码的一种相对直接的调整是改用std::unique_ptr (避免使用newdelete手动内存管理)。

IDEOne link w/ code IDEOne 链接带代码

Note the ownership semantics where the stack owns the top node, which owns the node underneath, etc. You could emulate this with manually with raw pointers using new in the push function, and delete in the pop function.请注意堆栈拥有顶部节点的所有权语义,它拥有下面的节点等。您可以使用原始指针手动模拟这一点,在 push 函数中使用new ,在 pop 函数中使用delete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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