linked list error

Im trying to write a program to make a generic linked list but im coming across some errors.

header:
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
#ifndef LLList.h
#define LList.h
#include <iostream>
template<class itemtype>
class llist
{
      friend std::ostream& operator<<(std::ostream & , llist<itemtype> &);
      private:
      {
              struct nodetype
              {
                     itemtype item;
                     nodetype * next;
              }
              nodetype *first, *last
      }
      public:
             llist();
             void firstinsert(itemtype);
             void frontinsert(itemtype);
             void endinsert(itemtype);
             void inorderinsert(itemtype);
             bool search(itemtype);
             bool delete(itemtype);
             bool listempty();{return(first==null)}
             itemtype firstitem();{return first->item}
             itemtype lastitem();{return last->item}
             ~llist();
};
#endif 


implementation
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
template <class itemtype>
llist<itemtype>::llist()
{
   first=last=null;
}
template <class itemtype>
void llist<itemtype>::firstinsert(itemtype x)
{
     first= new nodetype;
     last= first;
     first->item =x;
     first->next = null;
}
template <class itemtype>
void llist<itemtype>::frontinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=first;
     first=newnode;
}
template <class itemtype>
void llist<itemtype>::endinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=null;
     last->next=newnode;
     last=newnode;
}
template <class itemtype>
void llist<itemtype>::inorderinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=null;
     if(x<=first->item)
     {
       newnode->next=first;
       first=newnode;
     }
     else if (x>=last->item)
     {
      last->next=newnode;
     last=newnode;
     }
     else
     {
         prev=first;
         cur=first->next;
         while(x>cur->item)
         {
          prev=cur;
          cur=cur->next;
          }
     }
}
template <class itemtype>
bool llist<itemtype>::search(itemtype x)
{
     nodetype* cur;
     cur=first;
     while ((cur!=null)&&(x!=cur->item))
     {
           cur=cur->next;
     }
     return(cur!=null);
}
template <class itemtype>
bool llist<itemtype>::delete(itemtype &x)
{
     while(first!=null)
     {
       delnode=first;
       first=first->next;
       delete delnode;
     }
}
template <class itemtype>
bool llist<itemtype>::delete(itemtype x)
{
     nodetype *cur, *prev;
     prev=null;
     cur=first;
      while ((cur!=null)&&(x!=cur->item))
     { 
            prev=cur;
            cur=cur->next;
            if(cur==null)
                return 0;
            else
            {
                delnode=cur;
                if(prev==null)
                {   
                    first=first->next;
                }
                else
                {
                prev->next=cur->next;
                if (cur==last)
                   last=prev;
                }
                delete delnode;
                return 1;
            }
     }
}
ostream &operator <<(ostream &output,llist<itemtype> &l)
{
        llist<itemtype>::nodetype *cur;
        cur=l.first;
        while(cur!=null)
        {
           output<<cur->item<<"|";
           cur=cur->next;
           }
        reutrn output;
}               


my errors essentially boil down to:

7 E:\373 crap\LList.cpp expected init-declarator before '<' token
7 E:\373 crap\LList.cpp expected `;' before '<' token

except the first and last sections where i get:
2 E:\373 crap\LList.cpp expected constructor, destructor, or type conversion before '<' token

any help is appreciated.
Well, you have to #include the header file in the .cpp file, but ... this isn't going to work. You need
to move the implementation of all the methods to the header file.
ok,i actually added the #include now i get:

boiled down to :
3 LList.cpp expected unqualified-id before '.' token
3 LList.cpp expected `;' before '.' token

per section


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "llist.h"
template <class itemtype>
llist<itemtype>::llist()
{
   first=last=null;
}
template <class itemtype>
void llist<itemtype>::firstinsert(itemtype x)
{
     first= new nodetype;
     last= first;
     first->item =x;
     first->next = null;
}
template <class itemtype>
void llist<itemtype>::frontinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=first;
     first=newnode;
}
template <class itemtype>
void llist<itemtype>::endinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=null;
     last->next=newnode;
     last=newnode;
}
template <class itemtype>
void llist<itemtype>::inorderinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=null;
     if(x<=first->item)
     {
       newnode->next=first;
       first=newnode;
     }
     else if (x>=last->item)
     {
      last->next=newnode;
     last=newnode;
     }
     else
     {
         prev=first;
         cur=first->next;
         while(x>cur->item)
         {
          prev=cur;
          cur=cur->next;
          }
     }
}
template <class itemtype>
bool llist<itemtype>::search(itemtype x)
{
     nodetype* cur;
     cur=first;
     while ((cur!=null)&&(x!=cur->item))
     {
           cur=cur->next;
     }
     return(cur!=null);
}
template <class itemtype>
bool llist<itemtype>::delete(itemtype &x)
{
     while(first!=null)
     {
       delnode=first;
       first=first->next;
       delete delnode;
     }
}
template <class itemtype>
bool llist<itemtype>::delete(itemtype x)
{
     nodetype *cur, *prev;
     prev=null;
     cur=first;
      while ((cur!=null)&&(x!=cur->item))
     { 
            prev=cur;
            cur=cur->next;
            if(cur==null)
                return 0;
            else
            {
                delnode=cur;
                if(prev==null)
                {   
                    first=first->next;
                }
                else
                {
                prev->next=cur->next;
                if (cur==last)
                   last=prev;
                }
                delete delnode;
                return 1;
            }
     }
}
ostream &operator <<(ostream &output,llist<itemtype> &l)
{
        llist<itemtype>::nodetype *cur;
        cur=l.first;
        while(cur!=null)
        {
           output<<cur->item<<"|";
           cur=cur->next;
           }
        reutrn output;
}               


sorry to be annoying,but im not a good programmer.any help is appreciated.
since you are using templates you have to put the definitions into the header file along with all your declarations. the compiler will not compile since the data types in templates are not defined until they are used.
since you are using templates you have to put the definitions into the header file along with all your declarations. the compiler will not compile since the data types in templates are not defined until they are used.


dude,can you be a bit more specifc? I am a really horrible programmer.

Implementation
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "list.h"


template <class itemtype>
llist<itemtype>::llist()
{  
             
   first=last=0;
}
template <class itemtype>
void llist<itemtype>::firstinsert(itemtype x)
{
     first= new nodetype;
     last= first;
     first->item =x;
     first->next = 0;
}
template <class itemtype>
void llist<itemtype>::frontinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=first;
     first=newnode;
}
template <class itemtype>
void llist<itemtype>::endinsert(itemtype x)
{
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=0;
     last->next=newnode;
     last=newnode;
}
template <class itemtype>
void llist<itemtype>::inorderinsert(itemtype x)
{
     nodetype* cur;
     nodetype* prev;
     
     nodetype* newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=0;
     if(x<=first->item)
     {
       newnode->next=first;
       first=newnode;
     }
     else if (x>=last->item)
     {
      last->next=newnode;
     last=newnode;
     }
     else
     {
         prev=first;
         cur=first->next;
         while(x>cur->item)
         {
          prev=cur;
          cur=cur->next;
          }
     }
}
template <class itemtype>
bool llist<itemtype>::listempty()
{
return(first==0);
}
template <class itemtype>
itemtype llist<itemtype>::firstitem()
{
return first->item;
}
template <class itemtype>
itemtype llist<itemtype>::firstitem()
{
return last->item;
}
template <class itemtype>
bool llist<itemtype>::search(itemtype x)
{
     nodetype *cur;
     cur=first;
     while ((cur!=0)&&(x!=cur->item))
     {
           cur=cur->next;
     }
     return(cur!=null);
}
template <class itemtype>
bool llist<itemtype>::~llist()
{
     while(first!=0)
     {
       delnode=first;
       first=first->next;
       delete delnode;
     }
}
template <class itemtype>
bool llist<itemtype>::delete(itemtype x)
{
     nodetype *cur, *prev;
     prev=null;
     cur=first;
      while ((cur!=0)&&(x!=cur->item))
     { 
            prev=cur;
            cur=cur->next;
            if(cur==0)
                return 0;
            else
            {
                delnode=cur;
                if(prev==0)
                {   
                    first=first->next;
                }
                else
                {
                prev->next=cur->next;
                if (cur==last)
                   last=prev;
                }
                delete delnode;
                return 1;
            }
     }
}
std::ostream &operator <<(std::ostream &output,llist<itemtype> &l)
{
        llist<itemtype>::nodetype *cur;
        cur=l.first;
        while(cur!=0)
        {
           output<<cur->item<<"|";
           cur=cur->next;
           }
        return output;
}               


Header:
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
#ifndef LList.h
#define LList.h
#include <iostream>
template<class itemtype>
class llist
{
      friend std::ostream& operator<<(std::ostream & , llist<itemtype> &);
      private:
      {
              struct nodetype
              {
                     itemtype item;
                     nodetype *next;
              }
              nodetype *first, *last;
       }
      public:
             llist();
             void firstinsert(itemtype);
             void frontinsert(itemtype);
             void endinsert(itemtype);
             void inorderinsert(itemtype);
             bool search(itemtype);
             bool delete(itemtype);
             bool listempty();
             itemtype firstitem();
             itemtype lastitem();
             ~llist();
};
#endif 


please help
Take all of the code you put in the implementation file and paste it into the header file, then delete the implementation file.
anyway it could work by using the implementation file? or is it not possible? because i need to use the implementation file for the class.
since you are using templates you have to put the implementation into the header file.
ok,im now having problems that says that all of the declarations that i included in my private class is being shown as undeclared,along with a whole bunch of other errors.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef LList_h
#define LList_h
#include <iostream>
template<class itemtype>
class llist
{
      friend std::ostream& operator<<(std::ostream & , llist<itemtype> &)
      private:
      {
              struct nodetype
              {
                     itemtype item;
                     nodetype *next;
                 }
      nodetype *first, *last;
      }
      public:
             llist();
             void firstinsert(itemtype);
             void frontinsert(itemtype);
             void endinsert(itemtype);
             void inorderinsert(itemtype);
             bool search(itemtype);
             bool delete();
             bool listempty();
             itemtype firstitem();
             itemtype lastitem();
             ~llist();
};
#endif

template <class itemtype>
llist llist<itemtype>::llist()
{  
             
   first=last=0;
}
template <class itemtype>
void llist<itemtype>::firstinsert(itemtype x)
{
     nodetype *first
     first= new nodetype;
     last= first;
     first->item =x;
     first->next = 0;
}
template <class itemtype>
void llist<itemtype>::frontinsert(itemtype x)
{
     nodetype *newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=first;
     first=newnode;
}
template <class itemtype>
void llist<itemtype>::endinsert(itemtype x)
{
     nodetype *last;
     nodetype *newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=0;
     *last->next=newnode;
     *last=newnode;
}
template <class itemtype>
void llist<itemtype>::inorderinsert(itemtype x)
{
     nodetype *cur;
     nodetype *prev;
     
     nodetype *newnode;
     newnode= new nodetype;
     newnode->item =x;
     newnode->next=0;
     if(x<=first->item)
     {
       newnode->next=first;
       first=newnode;
     }
     else if (x>=last->item)
     {
      last->next=newnode;
     last=newnode;
     }
     else
     {
         prev=first;
         cur=first->next;
         while(x>cur->item)
         {
          prev=cur;
          cur=cur->next;
          }
     }
}
template <class itemtype>
bool llist<itemtype>::listempty()
{
return(first==0);
}
template <class itemtype>
itemtype llist<itemtype>::firstitem()
{
return first->item;
}
template <class itemtype>
itemtype llist<itemtype>::firstitem()
{
return last->item;
}
template <class itemtype>
bool llist<itemtype>::search(itemtype x)
{
     nodetype *cur;
     cur=first;
     while ((cur!=0)&&(x!=cur->item))
     {
           cur=cur->next;
     }
     return(cur!=null);
}
template <class itemtype>
bool llist<itemtype>::~llist()
{
     nodetype *delnode;
     while(first!=0)
     {
       delnode=first;
       first=first->next;
       delete delnode;
     }
}
template <class itemtype>
llist<itemtype>::delete(itemtype x)
{
     nodetype *cur, *prev;
     prev=null;
     cur=first;
      while ((cur!=0)&&(x!=cur->item))
     { 
            prev=cur;
            cur=cur->next;
            if(cur==0)
                return 0;
            else
            {
                delnode=cur;
                if(prev==0)
                {   
                    first=first->next;
                }
                else
                {
                prev->next=cur->next;
                if (cur==last)
                   last=prev;
                }
                delete delnode;
                return 1;
            }
     }
}
std::ostream &operator <<(std::ostream &output,llist<itemtype> &l)
{
        llist<itemtype>::nodetype *cur;
        cur=l.first;
        while(cur!=0)
        {
           output<<cur->item<<"|";
           cur=cur->next;
           }
        return output;
}               


8 F:\373\LList.h [Warning] friend declaration `std::ostream& operator<<(std::ostream&, llist<itemtype>&)' declares a non-template function
8 F:\373\LList.h [Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
8 F:\373\LList.h expected `;' before "private"
17 F:\373\LList.h expected `;' before "public"
24 F:\373\LList.h expected unqualified-id before "delete"


and to sort of boil it down:
F:\373\LList.h In constructor `llist<itemtype>::llist()':
36 F:\373\LList.h `first' undeclared (first use this function)
36 F:\373\LList.h `last' undeclared (first use this function)
F:\373\LList.h In member function `void llist<itemtype>::firstinsert(itemtype)':
41 F:\373\LList.h `nodetype' undeclared (first use this function)
42 F:\373\LList.h `first' undeclared (first use this function)
42 F:\373\LList.h expected `;' before "first"
43 F:\373\LList.h `last' undeclared (first use this function)
F:\373\LList.h In member function `void llist<itemtype>::frontinsert(itemtype)':
50 F:\373\LList.h `nodetype' undeclared (first use this function)
50 F:\373\LList.h `newnode' undeclared (first use this function)
51 F:\373\LList.h `nodetype' has not been declared
53 F:\373\LList.h `first' undeclared (first use this function)


please help me
missing semicolon line 7 and 14;

#endif on line 30 should be at EOF, not there.
Topic archived. No new replies allowed.