Reputation: 43
I am trying to compile the code my instructor has given us (had to retype it, but I can't find any typos) and it will not compile. We will be using this code for later assignments so I want to make it work before we get there.
It should simply make a linked-list based stack. I understand how the code works, and I have done templates before, but I can't figure out why it won't compile.
First 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
Stack.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;
}
and 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;
}
The errors I get are
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
Upvotes: 1
Views: 2032
Reputation: 92271
This syntax
template<class ItemType>
struct NodeType<ItemType>; //Line 9
would possibly be for partially specializing some existing NodeType
.
To (forward) declare the type, you just need
template<class ItemType>
struct NodeType;
Upvotes: 3