Problem with a Friend Function.

First off have this header file that I'm using in my main:

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

template<class T>

class ORDEREDLL
{
private:
	struct NODE
	{
		T info;
		NODE *next;
	};

	NODE *list;

public:
	ORDEREDLL();
	void Insert(T x);
	void display();
	void DeleteNode(T x);
	friend void UNION(ORDEREDLL a, ORDEREDLL b, ORDEREDLL AvB);
	~ORDEREDLL(){}
};

template<class T>
ORDEREDLL<T>::ORDEREDLL()
{
	list = NULL;
}

template<class T>
void ORDEREDLL<T>::Insert(T x)
{
	NODE *p = list; 
	NODE *q = list; 
	NODE *r;
	r = new(NODE); 
	r->info = x;
	r->next = NULL;

	while(p != NULL && p->info < x)
	{
		q = p; 
		p = p->next;
	}

	if (p == list)
	{
		list = r; 
		r->next = p;
	}

	else if (p == NULL)
	{
		q->next = r;
	}

	else
	{
		r->next = p; 
		q->next = r;
	}
}

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

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)
	{
		list = p->next;
		delete(p);
	}

	else
	{
		r->next = p->next;
		delete(p);
	}
}

template<class T>
void UNION(ORDEREDLL<T> a, ORDEREDLL<T> b, ORDEREDLL<T> AvB)
{
	while(a.list != NULL && b.list != NULL)
	{
		if(a.list->info == b.list->info)
		{
			AvB.Insert(a.list->info);
			a.list = a.list->next;
			b.list = b.list->next;
		}

		else if (a.list->info < b.list->info)
		{
			AvB.insert(a.list->info);
			a.list = a.list->next;
		}

		else
		{
			AvB.insert(b.list->info);
			b.list = b.list->next;
		}
	}
}


#endif 


Secondly here is my main:
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
#include <iostream>
#include "OrderedLL.h"
#include <ctime>
using namespace std;

int main()
{
	ORDEREDLL<int> X;
	ORDEREDLL<int> Y;
	ORDEREDLL<int> Z;
	int a[5] = {5,9,11,7,2};
	int b[4]={2,5,11,4};
	int t=0;

	for (int i=0; i<5; i++)
	{
		t = a[i];
		X.Insert(t);
	}

	for (int i=0; i<4; i++)
	{
		t = b[i];
		Y.Insert(t);
	}

	X.display();
	Y.display();

	UNION(X, Y, Z);

	Z.display();

	return 0;
}


The Error I'm getting when i try to run it is:
CPSC 131 Project 6-2.obj : error LNK2019: unresolved external symbol "void __cdecl UNION(class ORDEREDLL<int>,class ORDEREDLL<int>,class ORDEREDLL<int>)" (?UNION@@YAXV?$ORDEREDLL@H@@00@Z) referenced in function _main

TL;DR oh well

if you take the time to look at this thank you, could be something really simple I'm just not experienced enough to see it.
Last edited on
The linker is telling you it can't find UNION. This is because UNION is implictly declared at line 30 as a simple function, but you declared UNION to be specialized by type T in your header file. The two are not the same.
Line 30 should be:
 
UNION<int>(X, Y, Z);


You also have some other issues:
.h line 15. list is declared as private. You can't modify it from inside UNION, since UNION is not a member of ORDEREDLL.

.h line 115, line 121: insert is capitialized in ORDEREDLL.

.h line 102: A, B and AvB are passed by value. This means any modifications to them are lost when UNION exists. It's also inefficient. They should be passed bvy reference.

BTW, your title refers to a problem with a friend function, but I don't see the friend keyword anywhere.
Last edited on
Yeah man, I figured it all out after some tinkering. Thanks though you hit the nail on the head with what was wrong.

BTW, your title refers to a problem with a friend function, but I don't see the friend keyword anywhere.


sorry I don't really follow what you mean here.

thanks for taking the time though.
I am having similar issues. @Kentol, since you figured it out, do you think you could help me with my post which is about 4 posts below yours? Nobody has responded to it yet, and my program is due tomorrow. Do you happen to go to CSUF and have Ahmadnia as your teacher? hahahah.
Topic archived. No new replies allowed.