c++ - 创建链表,模板化 Stack

标签 c++

我正在尝试编译我的讲师给我们的代码(不得不重新输入,但我找不到任何拼写错误),但它无法编译。我们将在以后的作业中使用此代码,所以我想在我们到达那里之前让它工作。

它应该简单地创建一个基于链表的堆栈。我明白代码是如何工作的,我以前也做过模板,但我不明白为什么它不能编译。

第一个堆栈.h

#ifndef STACK_H
#define STACK_H
//Stack definition file
//Stack.h



template<class ItemType>
struct NodeType<ItemType>; //Line 9



template<class ItemType>
class StackType {
public:
    StackType();
    ~StackType();
    void MakeEmpty();
    void Push(ItemType);
    void Pop(ItemType &);
    bool IsEmpty() const;
    bool IsFull() const;
    ItemType Top();

private:
    NodeType* topPtr;
};


template<class ItemType>
struct NodeType<ItemType> {
    int info;
    NodeType<ItemType>* next;
}; //Line 34

#include "Stack.cpp"

#endif

堆栈.cpp

//Stack implemenation
#include <iostream>

template<class ItemType>
StackType<ItemType>::StackType() { //Line 5
    topPtr=NULL;
}


template <class ItemType>
StackType<ItemType>::~StackType() { //Line 11
    MakeEmpty();
}



template <class ItemType>
void StackType<ItemType>::MakeEmpty() {
    NodeType<ItemType>* tempPtr;

    while (topPtr != NULL) {
        tempPtr = topPtr;
        topPtr = topPtr->next;
        delete tempPtr;
    }
}

template <class ItemType>
void StackType<ItemType>::Pop(ItemType & item) {

    NodeType<ItemType>* tempPtr;
    item = topPtr->info;
    tempPtr = topPtr;
    topPtr = topPtr->next;
    delete tempPtr;
}

template<class ItemType>
void StackType<ItemType>::Push(ItemType item) {
    NodeType<ItemType>* location;
    location = new NodeType<ItemType>;
    location->info = newItem;
    location->next = topPtr;
    topPtr = location;
}

template <class ItemType>
bool StackType<ItemType>::IsEmpty() const {
    return (topPtr=NULL);
}

template <class ItemType>
bool StackType<ItemType>::IsFull() const {
    return (false);
}

template<class ItemType>
ItemType StackType<ItemType>::Top() {
        return topPtr->info;
}

和main.cpp

#include <iostream>
#include "Stack.h"
using namespace std;

int main () {

    int whatever;
    StackType<int> s;
    s.Push(10);
    s.Push(1);
    s.Pop(whatever);
    return 0;

}

我得到的错误是

c:\users\geldhart\dropbox\cs210\stack\stack.h(9):
error C2143: syntax error : missing ';' before '<'
c:\users\geldhart\dropbox\cs210\stack\stack.h(9):
error C2059: syntax error : '<'
c:\users\geldhart\dropbox\cs210\stack\stack.h(34):
error C2753: 'NodeType' : partial specialization cannot match argument list for primary template
Stack.cpp
c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5):
error C2143: syntax error : missing ';' before '<'
c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5):
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5):
error C2988: unrecognizable template declaration/definition
c:\users\geldhart\dropbox\cs210\stack\stack.cpp(5):
error C2059: syntax error : '<'
c:\users\geldhart\dropbox\cs210\stack\stack.cpp(11):
error C2588: '::~StackType' : illegal global destructor
c:\users\geldhart\dropbox\cs210\stack\stack.cpp(11):
fatal error C1903: unable to recover from previous error(s); stopping compilation

最佳答案

这种语法

template<class ItemType>
struct NodeType<ItemType>; //Line 9

可能用于部分特化一些现有的 NodeType

要(转发)声明类型,你只需要

template<class ItemType>
struct NodeType;

关于c++ - 创建链表,模板化 Stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10637355/

相关文章:

c++ - 编写一个函数来执行与 set_symmetric_difference 相同的工作

c++ - 16 到 32 位整数转换与性能

C++,隐式转换/构造函数是如何确定的?

c++ - OpenMP 开销计算

C++ 运算符 % 保证

c++ - 销毁对象放置新后未调用析构函数

c++ - 实现 C++ 到 lua 观察者模式?

c++ - getaddrinfo在程序中调用assert

c++ - 为什么调用的成员函数 mem_fun 必须是一个 const 函数?

c++ - 如何在VC++中运行以下mailslots程序