PLEASE LOOK AT MY CODE

Hi everyone, I'm getting a linker error on the following code, can somebody help me here.
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
#include <iostream>
#include <cstring>

using namespace std;

struct Node
{
    char info;
    Node* link;
};

template<class Type>
class unoderedLinkedList
{
public:
    unoderedLinkedList();
    void join(const unoderedLinkedList<Type>* firstList,
    const unoderedLinkedList<Type>* secondList);
    void insert(const Type& newItem);
    void print() const;
    friend void deleteOc(const unoderedLinkedList<Type>& L1, unoderedLinkedList<Type>& L2);
private:
    Node *first, *last, *current;
    int count;    
};  



int main()
{
    string name = "Sipho";
    string surname = "Van As";
    
    unoderedLinkedList<char> list1, list2, newList;
    int i = 0, num;
    
    unoderedLinkedList<int> lis1, lis2;
    
    cout << "Enter a list of integers ending by -1" <<endl;
    cin >> num;    
    while(num != -1)
    {
        lis1.insert(num);
        cin >> num;
    } 
    
    cout << "Enter list 2 of integers ending by -1" <<endl;
    cin >> num;    
    while(num != -1)
    {
        lis2.insert(num);
        cin >> num;
    } 
    
    while(name[i] != '\0')
    {
        list1.insert(name[i]);
        i++;
    }   
            
    list1.print();
    cout <<endl;
    int j = 0;
    while(surname[j] != '\0')
    {
        list2.insert(surname[j]);
        j++;
    } 
    
    list2.print();
    
    cout <<endl<<endl;
       
    newList.join(&list1, &list2);
    
    deleteOc(lis1, lis2);
    
    cout <<endl<<endl;
    cin.get();
    
    return 0;   
}

template<class Type>
unoderedLinkedList<Type>::unoderedLinkedList()
{
    first = NULL;
    last = NULL;
    current = NULL;
    count = 0;
}

template<class Type>
void unoderedLinkedList<Type>::join(const unoderedLinkedList<Type>* firstList,
 const unoderedLinkedList<Type>* secondList)
{
    Node* newNode = new Node;
    newNode->info = ' ';
    newNode->link = secondList->first;
    firstList->last->link = newNode;
    
    Node* cur;
    cur = firstList->first;
    
    while(cur != NULL)
    {
       cout << cur->info;
       cur = cur->link;
    }
}

template<class Type>
void unoderedLinkedList<Type>::insert(const Type& newItem)
{
     Node* newNode;
     
     newNode = new Node;
     newNode->info = newItem;
     newNode->link = NULL;
     
     if(first == NULL)
     {
         first = newNode;
         last = newNode;
         count++;
     }
     else
     {
         last->link = newNode;
         last = newNode;
         count++;
     }     
}

template<class Type>
void unoderedLinkedList<Type>::print() const
{
     Node* cur;
     
     cur = this->first;
     while(cur != NULL)
     {
         cout << cur->info;
         cur = cur->link; 
     }              
}

template<class Type>
void deleteOc(const unoderedLinkedList<Type>& L1, unoderedLinkedList<Type>& L2)
{
    Node* cur1, *cur2;
    Node* temp;
    
    cur1 = L1.first;
    cur2 = L2.first;
    
    while(L2.last != NULL)
    {
        while(L1.last != NULL)
        {
            if(cur2->info == cur1->info)    
            {
                temp = cur2;
                cur2 = cur2->link;
                delete temp;
            }
            else
                cur1 = cur1->link;
        }
        cur2 = cur2->link;
    }
}
You don't get a linker error on code. You get a linker error after the code is successfully compiled and for some reason the linker can put everything together for you.

Supplying the text of the error would be helpful.
you are declaring a non-template function as a friend, but you are defining a template function. My compiler tells me that you need to declare the template function before the friend statement and use <>. Of course, the declaration of the deleteOc function requires forward declaration of the class, so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...

template<class Type>
class unorderedLinkedList;

template<class Type>
void deleteOc(const unorderedLinkedList<Type>& L1, unorderedLinkedList<Type>& L2);

template<class Type>
class unorderedLinkedList
{
   ...
   friend void deleteOc<>(const unorderedLinkedList<Type>& L1, unorderedLinkedList<Type>& L2);
   ...
};

...


Last edited on
Topic archived. No new replies allowed.