### Problem with a Friend Function.

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128`` ``````#ifndef ORDEREDLL_H #define ORDEREDLL_H template 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 ORDEREDLL::ORDEREDLL() { list = NULL; } template void ORDEREDLL::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 void ORDEREDLL::display() { NODE *p = list; while( p != NULL) { cout<info<<"-->"; p = p->next; } cout<<"NULL\n"; } template void ORDEREDLL::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 void UNION(ORDEREDLL a, ORDEREDLL b, ORDEREDLL 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:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include "OrderedLL.h" #include using namespace std; int main() { ORDEREDLL X; ORDEREDLL Y; ORDEREDLL 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(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.