我正在尝试编译我的讲师给我们的代码(不得不重新输入,但我找不到任何拼写错误),但它无法编译。我们将在以后的作业中使用此代码,所以我想在我们到达那里之前让它工作。
它应该简单地创建一个基于链表的堆栈。我明白代码是如何工作的,我以前也做过模板,但我不明白为什么它不能编译。
第一个堆栈.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/