list node template problem

I'm trying to write a simple template for nodes that can be used to make a binary tree. There's a compiler error, the problem is caused by the visit and the traverse function I guess. The search didn't help. The results only answered questions to problems using functions inside classes, but I want to use it outside.

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
#ifndef BINARYTREE_HPP_
#define BINARYTREE_HPP_

#include <iostream>

template<class T> struct node
{
    node(T const& x, node* l, node* r){
        item=x;
        left=l;
        right=r;
    }

    ~node(){
        if(left!=NULL){
            delete left;
            left=NULL;
        }
        if(right!=NULL){
            delete right;
            right=NULL;
        }
    }

    T item;
    node* left;
    node* right;
};




void visit(node* Node){
    std::cout << Node->item<<std::endl;
}

void traverse_preorder(node* Node){
    if(Node==NULL) return;
    visit(Node);
    traverse(Node->left);
    traverse(Node->right);

}


#endif // #define BINARYTREE_HPP_ 


Compiler message:

1
2
3
4
5
6
binarytree.hpp:33:12: error: variable or field ‘visit’ declared void
binarytree.hpp:33:16: error: missing template arguments before ‘*’ token
binarytree.hpp:33:18: error: ‘Node’ was not declared in this scope
binarytree.hpp:37:24: error: variable or field ‘traverse_preorder’ declared void
binarytree.hpp:37:28: error: missing template arguments before ‘*’ token
binarytree.hpp:37:30: error: ‘Node’ was not declared in this scope
Last edited on
You use node as a type, but it's a template, so I think you mean to do this:

1
2
3
4
5
6
7
8
9
10
11
template<class T> void visit(node<T>* Node){
    std::cout << Node->item<<std::endl;
}

template<class T> void traverse_preorder(node<T>* Node){
    if(Node==NULL) return;
    visit(Node);
    traverse(Node->left);
    traverse(Node->right);

}

And I think you can then even use these two functions without the <...>.

That worked, thanks!
Topic archived. No new replies allowed.