Problem with friend function Union(), DisplaySet()

I am having trouble with a program where I am given a set A and a set B and I have to find the union of the two sets. I know my program is correct, but I am wondering why I am getting a linking error. Can somebody please help me?

//my OrderedLL.h file

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 OrderedLL_H
#define OrderedLL_H

template<class T>
class OrderedLL
{
private:
	
	struct NODE
	{
		T info;
		NODE *next;
	};
	
	NODE *list;

public:

	friend void Union(OrderedLL<T> a, OrderedLL<T> b, OrderedLL<T> &aUb);
	friend void DisplaySet(OrderedLL<T> set);

	//constructor
	OrderedLL();
	//insert item x
	void Insert(T x);
	//display the linked list
	void Display();
	//delete x from the linked list
	void DeleteNode(T x);
	//display a set
	void DisplaySet();
	//destructor
	~OrderedLL();

};

//constructor
template<class T>
OrderedLL<T>::OrderedLL()
{
	list = NULL;
}

//insert x in linked list
template<class T>
void OrderedLL<T>::Insert(T x)
{
	NODE *p = list, *q = list, *r;
	//create new node using r, then insert value of x and NULL terminate it
	r = new(NODE);
	r->info = x;
	r->next = NULL;
	
	while(p != NULL && p->info < x)
	{
		q = p;
		p = p->next;
	}
	
	if(p == list)//x is the first info
	{
		list = r;
		r->next = p;
	}
	else if(p == NULL)//x is the last info
	{
		q->next = r;
	}
	else//x is neither the first nor the last info
	{
		r->next = p;
		q->next = r;
	}
}

//display the linked list
template<class T>
void OrderedLL<T>::Display()
{
	NODE *p = list;
	
	while(p != NULL)
	{
		cout << p->info << "-->";
		p = p->next;
	}
	cout << "NULL\n";
}

//delete x from the linked list
template<class T>
void OrderedLL<T>::DeleteNode(T x)
{
	NODE *p = list, *r = list;

	while(p->info != x)
	{
		r = p;
		p = p->next;
	}

	if(p == list)
	{
		//delete the first node
		list = p->next;
		delete(p);
	}
	else
	{
		r->next = p->next;
		delete(p);
	}
}

//destructor
template<class T>
OrderedLL<T>::~OrderedLL()
{
	NODE *p = list;
	do
	{
		p = list;
		list = p->next;;
		delete(p);
	}while(list != NULL);
}

template<class T>
void Union(OrderedLL<T> a, OrderedLL<T> b, OrderedLL<T> &aUb)
{
	while(a.list != NULL && b.list != NULL)
	{
		if(a.list->info == b.list->info)
		{
			aUb.Insert(a.list->info);
			a.list = a.list->next;
			b.list = b.list->next;
		}
		else if(a.list->info < b.list->info)
		{
			aUb.Insert(a.list->info);
			a.list = a.list->info;
		}
		else//b.list->info < a.list->info
		{
			aUb.Insert(b.list->info);
			b.list = b.list->info;
		}
	}
	
	while(a.list != NULL)
	{
		aUb.Insert(a.list->info);
		a.list = a.list->next;
	}
	
	while(b.list != NULL)
	{
		aUb.Insert(b.list->info);
		b.list = b.list->next;
	}
}

template<class T>
void DisplaySet(OrderedLL<T> set)
{
	while(set.list != NULL)
	{
		cout << set.list->info << "->";
		set.list = set.list->next;
	}
	cout << "NULL" << endl;
}

#endif 


//my .cpp file

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
#include <iostream>
#include <ctime>
#include "OrderedLL.h"

//Function Prototypes
void Copy(int Set[], OrderedLL<int> &set, int counter);

using namespace std;

int main()
{
	time_t r;
	time(&r);
	cout << "Today is " << ctime(&r) << endl;

	int a[5] = {7, 5, 9, 2, 8};
	int a_count = 5;
	int b[4] = {2, 5, 11, 4};
	int b_count = 4;

	OrderedLL<int> setA, setB, set_union;

	Copy(a, setA, a_count);
	Copy(b, setB, b_count);

	DisplaySet(setA);
	DisplaySet(setB);

	Union(setA, setB, set_union);

	DisplaySet(set_union);

	return 0;
}

void Copy(int Set[], OrderedLL<int> &set, int counter)
{
	for(int i = 0; i < counter; i++)
	{
		set.Insert(Set[i]);
	}
}


//The error I get when I build

1>------ Build started: Project: Proj_6Part_2, Configuration: Debug Win32 ------
1>  Proj_6Part_2.cpp
1>c:\users\shawn\desktop\cpsc131\project6\proj_6part_2\proj_6part_2.cpp(14): warning C4996: 'ctime': This function or variable may be unsafe. Consider using ctime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\time.inl(86) : see declaration of 'ctime'
1>Proj_6Part_2.obj : error LNK2019: unresolved external symbol "void __cdecl Union(class OrderedLL<int>,class OrderedLL<int>,class OrderedLL<int> &)" (?Union@@YAXV?$OrderedLL@H@@0AAV1@@Z) referenced in function _main
1>Proj_6Part_2.obj : error LNK2019: unresolved external symbol "void __cdecl DisplaySet(class OrderedLL<int>)" (?DisplaySet@@YAXV?$OrderedLL@H@@@Z) referenced in function _main
1>C:\Users\Shawn\Desktop\CPSC131\Project6\Proj_6Part_2\Debug\Proj_6Part_2.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
Your friend functions are not template functions. So you did not define them. Instead of them you defined two template functions with the same names. Try to define non-template functions Union and DisplaySet where type int will be used instead of the template parameter T.
Last edited on
Topic archived. No new replies allowed.