Confused by Compiler error...

Hello!

Alright so this code is nearing completion, but I can't seem to get rid of this annoying compiler error and it is making absolutely no sense to me...

What the compiler is yelling at me about is on line 124 and it is saying "error: ‘class Link<int>’ has no member named ‘new_link’". This is absolutely confusing me because I am declaring "new_link" a couple lines up, and on top of that the error is not occurring at any of the other lines that I used new_link in that function, nor does it cause issues in the push_front() function.

Anyone know why this is happening? It's driving me insane! >.<

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

// List.h - a doubly-linked list

#include <algorithm>

using namespace std;

// forward declaration of classes defined in this header
template <class T> class Link;
template <class T> class List_iterator;

template <class T>
class List
{
public:
    typedef List_iterator<T> iterator;
    
    List();
    List(const List<T> & l);
    ~List();
    
    bool empty() const;
    unsigned int size() const;
    T & back() const;
    T & front() const;
    void push_front(const T & x);
    void push_back(const T & x);
    void pop_front();
    void pop_back();
    iterator begin() const;
    iterator end() const;
    void insert(iterator pos, const T & x); // insert x before pos
    void erase(iterator & pos);             // pos must be valid after erase() returns
    List<T> & operator=(const List<T> & l);
    
protected:
    Link<T> * first_link;
    Link<T> * last_link;
    unsigned int my_size;
};

template <class T>
List<T>::List()
{
    first_link = 0;
    last_link = 0;
    my_size = 0;
}

template <class T>
List<T>::List(const List & l)
{
    first_link = 0;
    last_link = 0;
    my_size = 0;
    for (Link<T> * current = l.first_link; current != 0; current = current -> next_link)
        push_back(current -> value);
}

template <class T>
List<T>::~List()
{
    while (!empty()){
        pop_back();
    }
}

template <class T>
bool List<T>::empty() const
{
    return my_size == 0; 
}

template <class T>
unsigned int List<T>::size() const
{
    int list_size = 0;
    for (Link<T> *ptr = first_link; ptr !=0; ptr = ptr -> next_link){
        list_size++;
    }
    return list_size;
}

template <class T>
T& List<T>::back() const
{
    return last_link;
}

template <class T>
T & List<T>::front() const
{
    return first_link;
}

template <class T>
void List<T>::push_front(const T & x)
{
    Link<T> * new_link = new Link<T>(x);
    if(empty()){
        first_link = last_link = new_link;
    }
    else{
        first_link -> next_link = new_link;
        new_link ->next_link =first_link;
        first_link = new_link;
    }
}

template <class T>
void List<T>::push_back(const T & x)
{
    Link<T> * new_link = new Link<T>(x);
    if(new_link == 0){
        cout << "out of memory\n";
        exit(1);
    }
    else if (first_link == 0){
        first_link = last_link = new_link;
    }
    else{
        last_link -> new_link = last_link;
        new_link -> prev_link = last_link;
        last_link = new_link;
    }
}

template <class T>
void List<T>::pop_front()
{
    Link<T> * temp = first_link;
    first_link = first_link->next_link;
    if (first_link != 0){
        first_link->prev_link = 0;
    }
    else{
        last_link = 0;
    }
    delete temp;
}

template <class T>
void List<T>::pop_back()
{
    my_size--;
}

template <class T>
typename List<T>::iterator List<T>::begin() const
{
    return iterator(first_link);
}

template <class T>
typename List<T>::iterator List<T>::end() const
{
    return iterator(last_link);
}


template <class T>
List<T> & List<T>::operator=(const List<T> & l)
{
    List_iterator<T> the_list;
    return l = the_list;
}

template <class T>
class Link
{
private:
    Link(const T & x): value(x), next_link(0), prev_link(0) {}
    
    T value;
    Link<T> * next_link;
    Link<T> * prev_link;
    //Link<T> * new_link;
    
    friend class List<T>;
    friend class List_iterator<T>;
};

template <class T> class List_iterator
{
public:
    typedef List_iterator<T> iterator;
    
    List_iterator(Link<T> * source_link): current_link(source_link) { }
    List_iterator(List<T> * t1, Link<T> * c1) : the_list(t1), current_link(c1) { }
    List_iterator(): current_link(0) { }
    List_iterator(List_iterator<T> * source_iterator): current_link(source_iterator.current_link) { }
    
    T & operator*();  // dereferencing operator
    iterator & operator=(const iterator & rhs){ the_list = rhs.the_list; current_link = rhs.current_link;}
    bool operator==(const iterator & rhs) const { return current_link == rhs.current_link; }
    bool operator!=(const iterator & rhs) const { return current_link != rhs.current_link; }
    iterator operator++();  // pre-increment, ex. ++it
    iterator & operator++(int); // post-increment, ex. it++
    iterator operator--();  // pre-decrement
    iterator & operator--(int); // post-decrement
    
protected:
    Link<T> * current_link;
    Link<T> * the_list;
    
    friend class List<T>;
};

template <class T>
T & List_iterator<T>::operator*()
{
    return current_link -> value;
}

template <class T>
List_iterator<T> & List_iterator<T>::operator++(int) // pre-increment
{
    current_link = current_link -> next_link;
    return *this;
}

template <class T>
List_iterator<T> List_iterator<T>::operator++()
{
    List_iterator<T> clone(the_list, current_link);
    current_link = current_link -> next_link;
    return clone;
}

template <class T>
List_iterator<T> & List_iterator<T>::operator--(int)
{
    current_link = current_link -> prev_link;
    return *this;
}



// more code goes here 
last_link -> new_link = last_link;

You are saying last_link->new_link, which means the compiler is looking that the last_link object, and looking for a new_link member in that class.

The class has not such member.

The new_link you think you have is a variable local to the push_back function. It is not part of the List<T> class.




This is probably a typo. Did you mean 'new_link' or did you mean 'next_link'?
What the compiler is yelling at me about is on line 124 and it is saying "error: ‘class Link<int>’ has no member named ‘new_link’". This is absolutely confusing me because I am declaring "new_link" a couple lines up, and on top of that the error is not occurring at any of the other lines that I used new_link in that function, nor does it cause issues in the push_front() function.


Yeah - but if you look at the push_back function and compare it to the push_front function function - you should see the that that particular line from the push_back function makes no sense (is wrong as the compiler points out).


Ah yeah Disch, you're right it was indeed a typo. I always tend to over think the most simple things and making it harder than what it is! lol

Any further critique of my functions would be much appreciated, as I'm still coding for erase and insert.

Thanks!
Topic archived. No new replies allowed.