1

I'm currently doing an assignment for course and it involves creating a template class. I have the template class set up and everything, but I'm getting errors that I can't seem to fix. I have tried various solutions and neither have worked so far, I have also done a reasonable amount of research and found no answers, precisely relevant to my scenario, concerning the problem although many others have asked.

Here is the code thus far...

#ifndef STACK_H
#define STACK_H

#include "Queue.h"
#include "MyException.h"

#include <iostream>

using namespace std;

template<class T>
class Stack;

template<class T>
ostream& operator<<(ostream&,Stack<T>&);

template<class T>
class Stack
{
  public:
    Stack();
    Stack(const Stack<T>& other);
    void push(const T& el);
    T pop();
    T peek();
    bool isEmpty();

    friend ostream& operator<< <T>(ostream&,Stack<T>&);

    Stack<T>& operator=(const Stack<T>& other);~Stack();

  private:
    class Node
    {
      public:
        Node(const T& data, Node* n = 0)
        {
            element = data;
            next = n;
        }

        T element;
        Node* next;
};

    Node* top;

};

#include "Stack.cpp"

#endif

that is the "Stack.h" and then there is

template<class T>
Stack<T>::Stack()
{

}

template <class T>
Stack<T>::Stack(const Stack<T>& other)
{

}

template <class T>
void Stack<T>::push(const T& el)
{

}

template <class T>
T Stack<T>::pop()
{

}

template <class T>
T Stack<T>::peek()
{

}

template <class T>
bool Stack<T>::isEmpty()
{
    return false;
}

that is the "Stack.cpp". I understand that the implementation being in a different file is not the generally accepted method, but unfortunately that is where the code needs to be. Now when I run this even without any real code, so to speak, i get the following errors.

3: expected constructor, destructor, or type conversion before '<' token
3: expected ';' before '<' token
9: expected constructor, destructor, or type conversion before '<' token
9: expected ';' before '<' token
17: expected init-declarator before '<' token
17: expected ';' before '<' token
23: expected init-declatator before '<' token
23: expected ';' before '<' token

and it goes on like that for each function up unto line 35.

Now, could it be my compiler? Could it be that I haven't finished all the functions and returned something yet?

Any help whatsoever would be largely appreciated, thanks.

Franken Steak
  • 60
  • 2
  • 9

2 Answers2

1

Don't compile Stack.cpp in a separate translation unit; it is already included in Stack.h.

clang++ -c Stack.h -std=c++11

works just fine.

Blaz Bratanic
  • 2,279
  • 12
  • 17
  • What if you don't have c++11? I am working in Visual Studio 2005 just because, :P – John Odom Feb 06 '14 at 17:23
  • You need a clang compiler. However, on windows i would prefer gcc over clang, since clang on windows is still pretty fresh. You can find one possible gcc build for windows on http://www.mingw.org. – Blaz Bratanic Feb 06 '14 at 17:57
  • But if I use gcc the command will work the same way? I will just have to change clang to gcc? – John Odom Feb 06 '14 at 19:15
0

The problem is that you are suppose to include the .h file in your .cpp file. So remove the

#include "Stack.cpp"

That is at the bottom of your header file and add

#include "Stack.h"

to the top of your .cpp file.

John Odom
  • 1,189
  • 2
  • 20
  • 35
  • I an not downvoting this answer however I believe a template class should not be implemented in a cpp file in almost all cases. – drescherjm Feb 06 '14 at 17:18
  • @drescherjm I have actually never worked with template classes. I have just tested his code in a blank project with the changes that me and Namfuak suggested and it builds fine. – John Odom Feb 06 '14 at 17:20
  • http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – drescherjm Feb 06 '14 at 17:28