0

Everything was working before I introduced templates to my code EDIT: Here is the problem to which I was able to narrow it down, thanks to your tips:

In file included from main.cpp:4: stack.cpp: In member function void Stack<TYPE>::push(Stack<TYPE>&, TYPE)': stack.cpp:35: error:node' is not a type

I wonder if a similar problem could appear later in the pop function, but it seems like it does not.

I'm confused as to why it seems to insist that node is not a type.

EDIT#2: this statement in the main.cpp file is now causing trouble. I have moved all the definitions out of stack.cpp to stack.h. After this Stack<int> list;my compiles says Segmentation fault (core dumped).

stack.h:

#include <iostream>

using namespace std;

template <typename TYPE>
struct node {
    TYPE data;
    node<Type> *next;
    node(){
        data = NULL;
        next = NULL;
    }
    ~node(){
        if (data!=0)
            delete next;
    }
    explicit node(int i){
        data = i;
    }
};
template <typename TYPE>
class Stack {
private:
    node<TYPE> *top;
    void init();
public:
    Stack(); // default constructor
    virtual ~Stack(); // destructor
    bool empty();
    void push(Stack&,TYPE);
    TYPE pop(Stack&);
    int peek();
    void clear();
    ostream& printf(ostream&, node<TYPE> *);
    ostream& print(ostream&);
    ostream& sequentialPrint(Stack&,ostream&);
    ostream& reversePrint(Stack&,ostream&);
    friend ostream& operator<<(ostream&, Stack&);

};

stack.cpp:

template <typename TYPE>
void Stack<TYPE>::push(Stack<TYPE> &s, TYPE i) {
    node<TYPE> * n = new node(i);
    n->next = top;
    top = n;
}
template <typename TYPE>
TYPE Stack<TYPE>::pop(Stack<TYPE> &s){
    if (empty()) {
        cerr<<"Stack is empty \n";
    }
    TYPE temp = s.top->data;
    top = top->next;
    return temp;
}
mzz
  • 1
  • 2
  • Please provide a [mcve] – Passer By Nov 14 '17 at 10:14
  • what do you mean? – mzz Nov 14 '17 at 10:16
  • @mzz click here -->> [mcve] – Jabberwocky Nov 14 '17 at 10:18
  • 1
    I meant the _Minimal_ part, primarily. You have many many functions irrelevant to the question you're asking – Passer By Nov 14 '17 at 10:18
  • I have no idea where the problem comes from, so I am unsure as to what to remove. – mzz Nov 14 '17 at 10:20
  • 1
    Remove as much as possible so you still get the error. – Jabberwocky Nov 14 '17 at 10:21
  • Trash `friend ostream& operator<<(ostream&, Stack&);` you don't needed. – Marek R Nov 14 '17 at 10:21
  • 2
    @mzz - If you aren't sure what to remove, do it bit by bit. You removed something and the problem persists? Good. You removed something and it's gone? You just narrowed it down. Continue until only the problem is distilled into an example. – StoryTeller - Unslander Monica Nov 14 '17 at 10:22
  • 1
    `#include "stack.cpp"` is pretty much always wrong - you shouldn't include cpp files directly (especially when your header file is also included and has no include guards) – UnholySheep Nov 14 '17 at 10:23
  • "A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header....Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you'll need" - https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – SChepurin Nov 14 '17 at 10:34
  • how would i solve this error: stack.h: In member function void Stack::push(Stack&, TYPE)': stack.h:48: error: node' is not a type ???? with your help i resolved the rest of issues, but this one persists – mzz Nov 14 '17 at 10:58

1 Answers1

0
  1. friend ostream& operator<<(ostream&, Stack&); is not needed
  2. you can't define template methods in cpp file. Every element of template which is template parameter depended must be defined in header file.
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 3
    _"you can't define template methods in cpp file"_ is not really true; you *can*, if the types to be instantiated are known in advance and explicitly instantiated in the `cpp` file. – underscore_d Nov 14 '17 at 10:28
  • you are saying about rare cases where template is used only with predefined set of types and this is not the case. – Marek R Nov 14 '17 at 10:31
  • does it mean that I need to fuse my stack.cpp and stack.h files? – mzz Nov 14 '17 at 10:32
  • @mzz That is most often the solution yes. – Passer By Nov 14 '17 at 10:36
  • @mzz yes. There was attempt to move template implementation to `cpp`, but it turn out the gain in build time was so small that idea was abandoned. – Marek R Nov 14 '17 at 10:43
  • how would i solve this error: stack.h: In member function `void Stack::push(Stack&, TYPE)': stack.h:48: error: `node' is not a type ???? with your help i resolved the rest of issues, but this one persists – mzz Nov 14 '17 at 10:48