Beginner Template .h and .cpp help

Hello all,

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.

The instructor gave us this header:
StackTest.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef STACKTEST_H
#define STACKTEST_H

/* this class defines a generic stack 
   make class Elem top level instead of an inner class */

#include <iostream>
using namespace 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() const throw(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 


My code is as follows:
StackTest.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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() const throw(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){}
You can't put template function bodies in a separate cpp file. They have to put in the .h file.
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
I think there's an "export" keyword or something, but next to no compilers support it.

The only other way would be to #include the cpp file either inside the header file, or alongside the header file.
the header file already has the .cpp included (line 72). I'm guessing she made a mistake? Unless someone else has another suggestion...
Ah, I didn't see that.

Here's what's happening:

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)
Topic archived. No new replies allowed.