//my_stack.h
#ifndef MY_STACK_H
#define MY_STACK_H
// my_stack.h -- a stack implemented on top of an STL vector
#include <vector>
#include <iostream>
#include <string>
usingnamespace std;
class StackException {
public:
StackException();
string message;
};
class StackEmptyException : public StackException {
public:
StackEmptyException();
};
class StackFullException : public StackException {
public:
StackFullException();
};
template <class T>
class my_stack
{
public:
my_stack();
bool empty() const;
unsignedint size() const;
void push(const T x) throw(StackFullException);
T pop() throw (StackEmptyException);
T top() throw (StackEmptyException);
private:
vector<T> container;
int length;
};
// Notice we include the .cpp here, this is because
// template classes are not to be compiled into .o
// files like regular source files, because the compiler
// won't know how to duplicate the code if they are.
#include "my_stack.cpp"
#endif
//errors :(
In file included from my_stack.h:50:0,
from test_stack.cpp:3:
my_stack.cpp:9:1: error: invalid use of template-name ‘my_stack’ without an argument list
my_stack.cpp:21:6: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp:21:23: error: non-member function ‘bool empty()’ cannot have cv-qualifier
my_stack.cpp: In function ‘bool empty()’:
my_stack.cpp:25:8: error: ‘container’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:31:14: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp:31:30: error: non-member function ‘unsignedint size()’ cannot have cv-qualifier
my_stack.cpp: In function ‘unsignedint size()’:
my_stack.cpp:35:8: error: ‘container’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:41:6: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp: In function ‘void push(T)’:
my_stack.cpp:49:4: error: ‘length’ was not declared in this scope
my_stack.cpp:49:14: error: ‘container’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:77:3: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp: In function ‘T pop()’:
my_stack.cpp:83:10: error: no matching function for call to ‘empty()’
my_stack.cpp:83:10: note: candidate is:
my_stack.cpp:21:6: note: template<class T> bool empty()
my_stack.cpp:91:8: error: ‘container’ was not declared in this scope
my_stack.cpp:93:1: error: ‘length’ was not declared in this scope
my_stack.cpp: At global scope:
my_stack.cpp:111:3: error: ‘template<class T> class my_stack’ used without template parameters
my_stack.cpp: In function ‘T top()’:
my_stack.cpp:117:10: error: no matching function for call to ‘empty()’
my_stack.cpp:117:10: note: candidate is:
my_stack.cpp:21:6: note: template<class T> bool empty()
my_stack.cpp:123:8: error: ‘container’ was not declared in this scope
my_stack.cpp:123:18: error: ‘length’ was not declared in this scope
test_stack.cpp: At global scope:
test_stack.cpp:7:5: warning: first argument of ‘int main(char**)’ should be ‘int’ [-Wmain]
test_stack.cpp:7:5: warning: ‘int main(char**)’ takes only zero or two arguments [-Wmain]