Template question
Dear all,
I have two functions:
The implementation code for the function prototypes is:Code:a) Member* get_member(Book* book); b)Book* get_book(Member* member);
a)
b)Code:template<class Book,class Member> Member* AssociationList<Book,Member>::get_member(Book* book) { Member* member=0; bool searching=true; int index=0; while(searching) { if (this->association_list[index]) if (this->association_list[index]->linked_book()==book) { member=this->association_list[index]->linked_member(); searching=false; } else index++; else index++; if (searching && (index == LIST_SIZE)) { searching=false; } } return member; }
All I want to do is use one generic function for the two, so that from the main.cpp:Code:template<class Book,class Member> Book* AssociationList<Book,Member>::get_book(Member* member) { Book* book=0; bool searching=true; int index=0; while(searching) { if (this->association_list[index]) if (this->association_list[index]->linked_member()==member) { book=this->association_list[index]->linked_book(); searching=false; } else index++; else index++; if (searching && (index == LIST_SIZE)) { searching = false; } } return book; }
...to call each one and dispaly either the books from a book array or the members from a member array.Code:void main() { List<Member>member1; List<Book>book1; char menuChoice; do { cin>>menuChoice; switch(menuChoice) { case '1': cout<<'\n'; book1.addElement("Books"); break; case '2': cout<<'\n'; break; case '3': cout<<'\n'; book1.displayElement("Books"); break; case '4': cout<<'\n'; /*member1.addElement("Member");*/ break; case '5': cout<<'\n'; break; case '6': cout<<'\n'; /*member1.displayElement("Member");*/ break; case '7': cout<<'\n'; //book1.borrowElement(); break; case '8': cout<<'\n'; //book1.returnElement(); break; default: cout<<'\n'; cout<<"Invalid Selection\n"; } }while(menuChoice != '0' && !cin.eof()); }
The'display' implementation code is:
But I'm having some errors trying to useCode:template<class Object> void List<Object>::displayElement(char* type) { if (num_elements == 0) cou t<<"No "<<type<<" is found in the "<<type<<"array.\n"; else for(int element=0; element<this->num_elements;element++) { cout<<'\n'; this->element_list[element]->displayassociation_list.get_member(element_list[element])); } }
The errors are:Code:template<class R,class P> R* get_member(P* pArg); instead of using the a,b function prototypes. The 'generic' function that I use and causes the problems is: template<class R,class P> R* AssociationList<Book,Member>::get_member(P* pArg) { R* member=0; bool searching=true; int index=0; while(searching) { if (this->association_list[index]) if (this->association_list[index]->linked_book()==book) { member=this->association_list[index]->linked_member(); searching=false; } else index++; else index++; if (searching && (index == LIST_SIZE)) { searching=false; } } return member; }
'AssociationList<class Book, class Member>::get_member':unable to resolve function overload
and the second one is:
template definitions cannot nest(??)
Could someone help me?
Regards,
grscot



LinkBack URL
About LinkBacks


