W
Will Oram
Hi -- me again. This time, I swear I have an on-topic post.
Again I apologise for any etiquette violations -- I don't use Usenet
frequently, so I'm not terribly aware of the minute rules. Bear with me
if I goof again.
Anyway, I have a program with two files, a header and a cpp main. The
header contains a class that handles a stack of ints (don't mind that I
simplified it down to a few lines for ease of reading). This code builds
fine.
// MAIN FILE
#include <iostream>
#include "stack2.h"
using namespace std;
int main () {
stack myStack;
return 0;
}
// -----------
// HEADER FILE
// FILE: stack2.h
#ifndef MAIN_SAVITCH_STACK2_H
#define MAIN_SAVITCH_STACK2_H
#include <cstdlib> // Provides NULL and size_t
#include "node2.h"
class stack {
public:
stack( ) { top_ptr = NULL; }
void push(const int& entry);
private:
int *top_ptr; // Points to top of stack
} };
#endif
// END CODE
However, I want to turn the above class into a template class. Tacking
on template <class Item> to the top of the class and changing int *top_
ptr to Item *top_ptr seems like it would be all that's necessary, but it
doesn't compile when I do this. The compiler complains that a) parse
error before ';', b) 'stack' is undeclared.
What is wrong? Thanks.
Again I apologise for any etiquette violations -- I don't use Usenet
frequently, so I'm not terribly aware of the minute rules. Bear with me
if I goof again.
Anyway, I have a program with two files, a header and a cpp main. The
header contains a class that handles a stack of ints (don't mind that I
simplified it down to a few lines for ease of reading). This code builds
fine.
// MAIN FILE
#include <iostream>
#include "stack2.h"
using namespace std;
int main () {
stack myStack;
return 0;
}
// -----------
// HEADER FILE
// FILE: stack2.h
#ifndef MAIN_SAVITCH_STACK2_H
#define MAIN_SAVITCH_STACK2_H
#include <cstdlib> // Provides NULL and size_t
#include "node2.h"
class stack {
public:
stack( ) { top_ptr = NULL; }
void push(const int& entry);
private:
int *top_ptr; // Points to top of stack
} };
#endif
// END CODE
However, I want to turn the above class into a template class. Tacking
on template <class Item> to the top of the class and changing int *top_
ptr to Item *top_ptr seems like it would be all that's necessary, but it
doesn't compile when I do this. The compiler complains that a) parse
error before ';', b) 'stack' is undeclared.
What is wrong? Thanks.