创建链表,模板化堆栈
Creating a linked list, templated Stack
我正在尝试编译我的老师给我们的代码(不得不重新输入它,但我找不到任何拼写错误(,它无法编译。我们将在以后的作业中使用此代码,因此我想在我们到达那里之前让它工作。
它应该简单地创建一个基于链接列表的堆栈。我了解代码的工作原理,并且我以前做过模板,但我无法弄清楚为什么它无法编译。
第一个堆栈.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;
- 在 C++ 中使用链表进行堆栈
- 实现基于链表的堆栈的基于范围的 for 循环
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- 使用链表在 c++ 中堆栈嵌入
- 使用链表实现堆栈时出错
- 未分配被释放的指针(将堆栈实现为链表时)
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 双链表堆栈删除函数不起作用
- 使用链表在堆栈中插入和删除
- C++ 中堆栈的链表实现
- c++ 中 if 语句中的多个条件(通过链表实现堆栈)
- 链表堆栈头文件中"<"标记之前的预期初始值设定项
- 链表与动态数组用于使用向量类实现堆栈
- 使用链表C++堆栈
- 链表堆栈类的复制构造函数:我的正在向后复制
- 尝试创建链表堆栈时出现链接器错误
- 学习在C++中实现链表堆栈类
- 反转使用链表实现的堆栈
- 菜单驱动的程序,堆栈为带字符信息的链表
- 需要修改堆栈/链表程序以确定单词是否为回文