Errors with template using nodes

Apr 9, 2014 at 8:46pm
In line 41 I am getting the error "a template declaration cannon appear at block scope" and at line 50 I am getting the error "expected ';' before 'template'". I am not exactly sure what I am doing wrong, any help would be appreciated. There are a couple more functions that I did not include cause I did not receive errors in them, as well I have a main that I did not include. Thanks.

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

#include <iostream>

using namespace std;

template <typename T>
void insert(Node<T> *head, T val) {
    Node<T> *p=head;
	Node<T> *q=NULL;
	while (p !=NULL && val < p->data) {
            q=p;
            p=p->next;
    }
    if (q==NULL)
        insertFront(head, val);
    else
        insertAfter(q, val);
return;
}

template <typename T>
void remove(Node<T> *head, T val) {
    Node<T> *p=head;
	Node<T> *q=NULL;
	while (p !=NULL) {
        if(p->data==val) {
            if(q==NULL)
                removeFront(p);
            else
                removeAfter(q);
        removeAfter(q);
        }
        else {
            q=p;
            p=p->next;
    }
}

template <typename T>
void print(ostream &os, Node<T> *head) {
	Node<T> *p = head;
	while (p) {
		os << *p;
		p = p->next;
    }
}

template <typename T>
int length(Node<T> *head) {
	int count = 0;
	Node<T> *p = head;
	while (p) {
		count++;
		p = p->next;
	}
	return count;
}
Apr 9, 2014 at 11:50pm
anyone have an idea what I'm doing wrong?
Apr 10, 2014 at 12:00am
Here's your remove function, reindented:
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
template <typename T>
void remove(Node<T> *head, T val) {
    Node<T> *p=head;
    Node<T> *q=NULL;
    while (p !=NULL) {
        if(p->data==val) {
            if(q==NULL)
                removeFront(p);
            else
                removeAfter(q);
            removeAfter(q);
        }
        else {
            q=p;
            p=p->next;
        }
    }

template <typename T>
void print(ostream &os, Node<T> *head) {
    // ...
}
Apr 10, 2014 at 12:08am
thanks... however I think that was just a copy and paste issue.
Apr 10, 2014 at 12:10am
A copy and paste issue that you're missing a closing curly brace for your remove function?
As it is right now in the code you posted, your print function is inside your remove function.
Last edited on Apr 10, 2014 at 12:10am
Apr 10, 2014 at 12:13am
haha, thanks I looked through my code to check it but didn't realize I missed that.
Topic archived. No new replies allowed.