This is probably a basic questions, but so far my searches have proved unsuccessful. I apologize if this question has already been asked but when I try to search the forums I am getting a server overload problem. This is also my first question in the forums, so please be gentle if I don't do something right. With that in mind, all suggestions are welcome!
I want the following to compile, however I am getting a "function template has already been defined" error for each function. *NOTE* As part of this assignment I am NOT allowed to change StackTest.h in any way.
#ifndef STACKTEST_H
#define STACKTEST_H
/* this class defines a generic stack
make class Elem top level instead of an inner class */
#include <iostream>
usingnamespace std;
template<class T>
struct Elem {
T info;
Elem<T> *next;
};
template<class T>
class Stack {
public:
// exceptions
class Overflow{};
class Underflow{};
Stack();
// constructs empty stack with a default
// number of elements available
Stack(const Stack<T> &v);
// copy constructor
~Stack();
// destructor
Stack<T>& operator =(const Stack<T> &v);
// assignment operator
void push(T value) throw(Overflow) ;
// pushes value onto top of stack
// throws exception if full
void pop() throw(Underflow);
// pops top value off stack
// throws exception if empty
T top() constthrow(Underflow);
// returns top value on stack
// throws exception if empty
bool empty() const;
// returns true if stack is empty
bool full() const;
// returns true if stack is full
void output(ostream &s) const;
// outputs elements of stack
private:
Elem<T> *head;
void copyCode(const Stack<T> & v);
// common code for copy constructor and assignment
void destructCode();
// common code for destructor and assignment
};
template<class T>
ostream & operator << (ostream & s, const Stack<T> & v);
// outputs using keyword operation
#include "StackTest.cpp"
#endif
#define STACK_SIZE 1000
#include "StackTest.h"
//default constructor
template<class T>
Stack<T>::Stack(){}
//copy constructor
template<class T>
Stack<T>::Stack(const Stack<T> &v){}
//destructor
template<class T>
Stack<T>::~Stack(){}
//assignment operator
template<class T>
Stack<T>& Stack<T>::operator =(const Stack<T> &v){}
//pushes an element of type T onto the stack
template<class T>
void Stack<T>::push(T value) throw(Overflow){}
//pops an element of type T from the stack
template<class T>
void Stack<T>::pop() throw(Underflow){}
//displays top element of the stack
template<class T>
T Stack<T>::top() constthrow(Underflow){}
//returns true if the stack is empty
template<class T>
bool Stack<T>::empty() const{}
//returns true if the stack is full
template<class T>
bool Stack<T>::full() const{}
// common code for copy constructor and assignment
template<class T>
void Stack<T>::copyCode(const Stack<T> & v){}
// common code for destructor and assignment
template<class T>
void Stack<T>::destructCode(){}
// outputs elements of stack
template<class T>
void Stack<T>::output(ostream &s) const{}
// outputs using keyword operation
template<class T>
ostream & operator << (ostream & s, const Stack<T> & v){}
Is there a way around this? The instructor has specifically told us we need to have a .cpp file and a .h file, and the .h file has to be the one provided
StackTest.cpp is including StackTest.h
-- StackTest.h defines the class body
-- StackTest.h includes StackTest.cpp
---- StackTest.cpp defines all function bodies
---- StackTest.cpp is done being compiled (resume compiling StackTest.h)
-- StackTest.h is done being compiled (resume compiling StackTest.cpp)
StackTest.cpp defines all function bodies (again)
As a result, the function bodies are all being defined twice, hence your error. This is one reason why including cpp files a bad idea (although your instructor isn't giving you any choice.. grumble grumble)
2 possible solutions:
1) Put include guards in your StackTest.cpp file (#ifndef STACKTEST_CPP)
or
2) Don't compile StackTest.cpp (remove it from the project or tell the IDE not to compile it on its own)