创建链表,模板化堆栈

Creating a linked list, templated Stack

本文关键字:堆栈 链表 创建      更新时间:2023-10-16

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

它应该简单地创建一个基于链接列表的堆栈。我了解代码的工作原理,并且我以前做过模板,但我无法弄清楚为什么它无法编译。

第一个堆栈.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;
}

和主要.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(:
错误 C2143:语法错误:<">
之前缺少";" C:\users\geldhart\dropbox\cs210\stack\stack.h(9(:
错误 C2059:语法错误:"<">
C:\users\geldhart\dropbox\cs210\stack\stack.h(34(:
错误 C2753:"NodeType":部分专用化无法与主模板
的参数列表匹配 堆栈.cpp
C:\users\geldhart\dropbox\cs210\stack\stack.cpp(5(:
错误 C2143:语法错误:<">
之前缺少";" C:\users\geldhart\dropbox\cs210\stack\stack.cpp(5(:
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持默认整数
C:\users\geldhart\dropbox\cs210\stack\stack.cpp(5(:
错误 C2988:无法识别的模板声明/定义
C:\users\geldhart\dropbox\cs210\stack\stack.cpp(5(:
错误 C2059:语法错误:"<">
C:\users\geldhart\dropbox\cs210\stack\stack.cpp(11(:
错误 C2588:"::~堆栈类型":非法的全局析构函数
C:\users\geldhart\dropbox\cs210\stack\stack.cpp(11(:
致命错误 C1903:无法从以前的错误中恢复;停止编译

此语法

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

可能是部分专业化一些现有的NodeType.

要(转发(声明类型,您只需要

template<class ItemType>
struct NodeType;