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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175`` ``````#ifndef OrderedLL_H #define OrderedLL_H template class OrderedLL { private: struct NODE { T info; NODE *next; }; NODE *list; public: friend void Union(OrderedLL a, OrderedLL b, OrderedLL &aUb); friend void DisplaySet(OrderedLL 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 OrderedLL::OrderedLL() { list = NULL; } //insert x in linked list template void OrderedLL::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 void OrderedLL::Display() { NODE *p = list; while(p != NULL) { cout << p->info << "-->"; p = p->next; } cout << "NULL\n"; } //delete x from the linked list template void OrderedLL::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 OrderedLL::~OrderedLL() { NODE *p = list; do { p = list; list = p->next;; delete(p); }while(list != NULL); } template void Union(OrderedLL a, OrderedLL b, OrderedLL &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 void DisplaySet(OrderedLL set) { while(set.list != NULL) { cout << set.list->info << "->"; set.list = set.list->next; } cout << "NULL" << endl; } #endif ``````

//my .cpp file

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include #include "OrderedLL.h" //Function Prototypes void Copy(int Set[], OrderedLL &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 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 &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,class OrderedLL,class OrderedLL &)" (?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)" (?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.