Need some help fast with Template in Double Linked List

Ok so i finally managed to finish a working DLL using only class...anyway now i have to use template. So here is what i tried:
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
#include <cstdlib>
#include <iostream>
using namespace std;
template <class Tip>
class nod{
      nod<Tip>* next;
      nod<Tip>* back;
      public:
             Tip x;
             nod(Tip a){back=NULL;x=a;next=NULL;}      
             friend class Lista;
             friend class ListaD;
      };     
template <class Tip>
class Lista{
public:
      nod<Tip>* first;
      nod<Tip>* last;
      Lista(){first=NULL;last=NULL;}       //-constructor
      Lista(Tip x){first=NULL;last=NULL;insert(x);}
      void creare();                       //Creates the list
      bool isEmpty(){return (first==NULL);}    //-Verifies if its empty
      virtual void insert(Tip x);   
      void afisare(){nod<Tip>* c;c=first;   while(c!=NULL){ cout<<c->x<<" ";c=c->next;} cout<<endl;} // Shows the whole list from right to left
      ~Lista();   //-destructor
};
template <class Tip>
void Lista<Tip>::creare()
{
 cout<<"How many elements should it contain? "<<endl;
 int n;
 Tip x;
 cin>>n;
 for(int i=1;i<=n;i++)
 {
 cout<<"Insert the element: (You have "<<(n-i+1)<<" elements left to insert)"<<endl;
 cin>>x;
 insert(x);
 }
}


template <class Tip>
void Lista<Tip>::insert(Tip x)
{

                   nod<Tip>* a=new nod<Tip>(x);
                   if(isEmpty())
                           {  first=a;
                              last=a;
                            }
                   else
                            {
                              last->next=a;
                              last=a;
                            }
                                                                 
}                      
template <class Tip>
Lista<Tip>::~Lista()
{
nod<Tip>* c;
               while(first!=NULL)
               {  
               c=first;
               first=first->next;
               delete c;               
               }
}


int main()

    Lista<int> A;
    return 0;
}


i get errors:

  In instantiation of `nod<int>': 
66 C:\Dev-Cpp\LP3 v2.cpp   instantiated from `Lista<Tip>::~Lista() [with Tip = int]' 
396 C:\Dev-Cpp\LP3 v2.cpp   instantiated from here 
9 C:\Dev-Cpp\LP3 v2.cpp template argument required for `struct Lista' 
 C:\Dev-Cpp\LP3 v2.cpp In destructor `Lista<Tip>::~Lista() [with Tip = int]': 
396 C:\Dev-Cpp\LP3 v2.cpp   instantiated from here 
6 C:\Dev-Cpp\LP3 v2.cpp `nod<int>*nod<int>::next' is private 
66 C:\Dev-Cpp\LP3 v2.cpp within this context 
 C:\Dev-Cpp\LP3 v2.cpp In member function `void Lista<Tip>::insert(Tip) [with Tip = int]': 
397 C:\Dev-Cpp\LP3 v2.cpp   instantiated from here 
6 C:\Dev-Cpp\LP3 v2.cpp `nod<int>*nod<int>::next' is private 
54 C:\Dev-Cpp\LP3 v2.cpp within this context 
  C:\Dev-Cpp\Makefile.win [Build Error]  ["LP3 v2.o"] Error 1   


Any help is appreciated.New to templates.(just 3 hours ago started reading about them)
Ty

Its actually a simple linked list. I deleted the double linked list (its inherited). I want to make this one work for now...
If main is empty. i get no errors...>.<
Last edited on
On line 9, the first error is saying your friend declarations need to have be a template instantiation; i.e. you can't be a friend of Lista, you need to be a friend of Lista<int> or some other instance.

On line 54 and 66, the member 'first' is private and can't be accessed.

Hmm, then how should i declare them friends? friend class<tip> Lista;
gives me " 'Lista' is not a template type ' " and some other errors.
And yes you are right with the private, Thank you! It works if i put public on all members. But how can i make it private and work? .
Thank you again
And one more question. With inheritance, should it work if i just add " template <class Tip>" like i did in this class?
Last edited on
ok i managed somehow by declaring Lista as a template first row, now the next problem is how to declare a inheritance .
I declared it
template <class Tip>
class ListaD: public Lista<Tip> {


but i get error at the end " first is undeclared
last is undeclared " . If i declare
Class ListaD: public Lista<int> it works o.O

Edit: Even the overloading operators works,everything is fine now ,except that it only works with <int> and not <Tip> , so i can work only with integers..Can anyone please help me with this? The inherited class is not taking anything from the Lista. It acts like a independent. Even IsEmpty() isn't working.
Last edited on
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
template <class Tip>
class ListaD : public Lista<Tip> {
      public:
      ListaD() : Lista<Tip>(){};
      void insert_back(Tip x);   
      void delete_last();  
      void delete_first();   
      void gasire_cheie(Tip k);    
      void delete_cheie(nod<Tip>* k);    
      void insert(Tip x);
       ~ListaD();
       ListaD& operator= (const ListaD &a);
       ListaD operator+ (const ListaD &a);
       ListaD(const ListaD &a)
       {
                   nod<Tip>* d = a.first ;
	               while(d!=NULL)
	               {
		           insert(d->x);
		           d=d->next;
	               }
       } 
      };
template <class Tip>
ListaD<Tip> & ListaD<Tip>::operator= (const ListaD & a)
{
	while(first!=NULL)
	{
		delete_first();
	}


	nod<Tip>* d = a.first ;
	while(d!=NULL)
	{
		insert(d->x);
		d=d->next;
	}

	return (*this) ;        
}
template <class Tip>

 
template <class Tip>
 ListaD<Tip> ListaD<Tip>::operator+ (const ListaD &a)
{
      ListaD art;
      nod<Tip>* c;
      nod<Tip>* d;
      Tip temp;
      d=a.first;
      c=first;
      while(c!=NULL && d!=NULL)
            {    
                
                 temp=((c->x)+(d->x));
                 art.insert(temp);
                 
            
            c=c->next;
            d=d->next;
            }
      return (art);
}
template <class Tip>
void ListaD<Tip>::delete_cheie(nod<Tip>* k)
{
      nod<Tip>* c;
      c=k->back;
      c->next=k->next;
      c=k->next;
      c->back=k->back;
      delete k;
}

template <class Tip>
void ListaD<Tip>::gasire_cheie(Tip k)
{
     nod<Tip>* c;
     nod<Tip>* p;
     int contur=0;
     c=first;
     while(c!=NULL)
     {
         if(k==c->x)
         {
         contur++;
         p=c;
              
         }
         c=c->next;
         
     }
     if(contur>1)
              {
              cout<<"In multimea exista "<<contur<<" elemente cu valoarea "<<k<<"."<<endl;
              cout<<"Pe al catelea doriti sa il stergeti?(Tastati 1,2,3...etc in functie de al catelea elem doriti)"<<endl;
              int choose;
              cin>>choose;
              while(choose<1 && choose>contur)
              {
              cout<<"Valoarea data nu exista!Reintroduceti!"<<endl;
              cin>>choose;
              }
              c=first;
                      while(c!=NULL)
                      { 
                                      if(k==c->x)
                                      {
                                                if(choose==1)
                                                {    
                                                     if(c!=last && c!=first)
                                                     {delete_cheie(c);}
                                                     else
                                                     {
                                                               if(c==last)
                                                               {
                                                               delete_last();
                                                               }
                                                               else
                                                               {
                                                               delete_first();
                                                               }
                                                     }
                                                     
                                                     break;
                                                }
                                                choose--;
                                      } 
                      c=c->next;
                      }
              }
     if(contur==1)
     {
                  if(p!=last && p!=first)
                  {
                  delete_cheie(p);
                  }
                  else
                  {
                      if(p==last)
                      {
                                 delete_last();
                      }
                      else
                      {
                         delete_first();
                      }
                  }
     }
     if(contur==0)
     {
     cout<<"Elementul cautat nu exista."<<endl;
     }
    
}

template <class Tip>
void ListaD<Tip>::insert_back(Tip x)
{
     nod<Tip>* a=new nod<Tip>(x);
                   if(isEmpty())
                            {  first=a;
                               last=a;
                            }
                   else 
                            {
                              first->back=a;
                              a->next=first;
                              first=a;
                            }
     
}

template <class Tip>
void ListaD<Tip>::delete_first()
{
              if(isEmpty())
              {
              cout<<"Eroare.Lista este goala."<<endl;             
              }
              else
              {
                  if(first->next==NULL)
                  {
                  delete first;
                  last=NULL;
                  first=NULL;
                   
                  }
                  else{
              nod<Tip>* c;
              c=first;
              first=first->next;
              first->back=NULL;    
              delete c;
              }
              }
}

template <class Tip>
void ListaD<Tip>::delete_last()
{
              if(isEmpty())
              {
              cout<<"Eroare.Lista este goala."<<endl;             
              }
              else
              {
                    if(last->back==NULL)
                    {
                    delete last;
                    last=NULL;
                    first=NULL;
                    }
                    else
                    {
                    nod<Tip>* c;
                    c=last;
                    last=last->back;
                    last->next=NULL;    
                    delete c;
                    }
              }
}

template <class Tip>
void ListaD<Tip>::insert(Tip x)
{

                   nod<Tip>* a=new nod<Tip>(x);
                   if(isEmpty())
                           {  first=a;
                              last=a;
                            }
                   else
                            {
                              last->next=a;
                              a->back=last;
                              last=a;
                            }
                                                                 
} 

template <class Tip>
ListaD<Tip>::~ListaD()
{
nod<Tip>* c;
               while(first!=NULL)
               {  
               c=first;
               first=first->next;
               delete c;               
               }
}
"Hmm, then how should i declare them friends? friend class<tip> Lista;
gives me " 'Lista' is not a template type ' " and some other errors."


Template friend class:
template <typename T> friend class Lista;

As for it only working for ints, I don't understand what you are saying completely. So this works...

ListaD<int> mylista;

But this doesn't??

ListaD<SomeClass> mylista;

What is confusing me is you are using the template type in your wording. Meaning, when you do ListaD<int>, where Tip = int. Are you truly saying you are doing ListaD<Tip>, where Tip = Tip?

Your declaration, see how this confuses the reader, can you elaborate or show how you are trying to instantiate the template as well as this "Tip" class?:
1
2
3
template <class Tip>
class ListaD : public Lista<Tip> {
      public:
I just wanted a Double Linked List inherited(derived) from the Simple Linked List using only classes. And i wanted to switch int/float/double whenever i want. Exemple:
1st List: ListD <int>A;
2nd List: ListD <float>B; etc..
Doing operations and such (I'm aware that i must make only int + int (A+B) with this kind of code).
Anyway just one type everywhere. I managed to mae the friend class work. Thank you ^^. i was missing a piece of code at the beggining of the code:
1
2
template <class Tip>
class Lista;
(thats needed before declaring friend class, or else i get "Lista is not a template".
Now i don't get it. If i insert the code written above on my last post, it doesn't work and says:" first is undeclared.... last is undeclared,..... a function IsEmpty() is necessary..." things like that. If i put class ListaD : public Lista<int> { replacing Tip with int it works perfectly. For now i just declared nod<Tip> last, nod<Tip> first, afisare() and isEmpty(). But they should be already be declared, being a inherited class right? Also weird thing is that the only thing inherited is the citire() function. (It works only if i redeclare them)
Sorry for my bad english,i hope you understood.
Last edited on
Topic archived. No new replies allowed.