Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Template question

Status
Not open for further replies.

grscot

Programmer
Apr 25, 2003
16
GR
Dear all,
I have two functions:
a) Member* get_member(Book* book);
b)Book* get_book(Member* member);

The implementation code for the function prototypes is:

a)
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;
}

b)
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;
}

All I want to do is use one generic function for the two, so that from the main.cpp:

void main()
{
List<Member>member1;
List<Book>book1;
char menuChoice;
do
{
cout<<'\n';
cout<<'\n';
cout<<&quot;**** Main Menu ****&quot;<<endl;
cout<<'\n';
cout<<&quot;1: Add Book &quot;<<endl;
cout<<'\n';
cout<<&quot;2: Remove Book &quot;<<endl;
cout<<'\n';
cout<<&quot;3: Display Books &quot;<<endl;
cout<<'\n';
cout<<&quot;4: Add Member &quot;<<endl;
cout<<'\n';
cout<<&quot;5: Remove Member &quot;<<endl;
cout<<'\n';
cout<<&quot;6: Display Members &quot;<<endl;
cout<<'\n';
cout<<&quot;7: Borrow Book &quot;<<endl;
cout<<'\n';
cout<<&quot;8: Return Book &quot;<<endl;
cout<<'\n';
cout<<&quot;0: Quit &quot;<<endl;
cout<<endl;
cout<<&quot;Enter one of the above choices: &quot;;cinFlush;
cin>>menuChoice;
switch(menuChoice)
{
case '1': cout<<'\n';
book1.addElement(&quot;Books&quot;);
break;
case '2': cout<<'\n';

break;
case '3': cout<<'\n';
book1.displayElement(&quot;Books&quot;);
break;
case '4': cout<<'\n';
/*member1.addElement(&quot;Member&quot;);*/
break;
case '5': cout<<'\n';

break;
case '6': cout<<'\n';
/*member1.displayElement(&quot;Member&quot;);*/
break;
case '7': cout<<'\n';
//book1.borrowElement();
break;
case '8': cout<<'\n';
//book1.returnElement();
break;
default: cout<<'\n';
cout<<&quot;Invalid Selection\n&quot;;
}
}while(menuChoice != '0' && !cin.eof());
prompt_to_quit();
}

...to call each one and dispaly either the books from a book array or the members from a member array.
The'display' implementation code is:
template<class Object>
void List<Object>::displayElement(char* type)
{
if (num_elements == 0)
cout<<&quot;No &quot;<<type<<&quot; is found in the &quot;<<type<<&quot; array.\n&quot;;
else
for(int element=0; element<this->num_elements; element++)
{
cout<<'\n';
this->element_list[element]->display(association_list.get_member(element_list[element]));
}
}

But I'm having some errors trying to use
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;
}
The errors are:
'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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top