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!

More template problems

Status
Not open for further replies.

grscot

Programmer
Apr 25, 2003
16
GR
Dear all,I'm really sorry for asking again about the same thing, but I don't really undersatnd what's going on with the templates.

I've tried to use the template<class R, class P>
R* get_member(P* pArg)
as adviced, but it seems that my program is more complicated.
So, I think I have to submit you a significant part of my code to give me an extra help (and final one I hope ) to overcome this awful template obstacle.

AssociationList.h
template<class Book,class Member>
class AssociationList
{
public:
//It initialises all slots in the associationlist array to zero.
AssociationList();

/* It searches the associationlist, if book is found then returns
the member that is connected with.*/
//Member* get_member(Book* book);

/* It searches the associationlist, if member is found then returns
the book that is connected with.*/
//Book* get_book(Member* member);

template<class R,class P>
R* get_member(P* pArg);

/* Checks that book/member not already linked
creates association if objects are free to link
returns whether or not link was valid */
bool link(Book* book,Member* member);

/* Checks that book and member are linked
deletes association if they are linked
returns whether or not unlinking was valid */
bool unlink(Book* book,Member* member);
private:
Association<Book,Member>* association_list[LIST_SIZE];
};

AssociationList.cpp
template<class Book,class Member>
AssociationList<Book,Member>::AssociationList()
{
cout<<&quot;AssociationList constructor called\n&quot;;
int index;

for(index=0; index<LIST_SIZE; index++)
this->association_list[index]=0;
}

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


//template<class Book,class Member>
//Member* AssociationList::get_member(Book* book)

/*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;
}

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;
}*/

Association.h
template<class Book,class Member>
class Association
{
public:
//Sets up book and member with parameters
Association(Book* book, Member* member);

//Returns Book
Book* linked_book(){return this->book;}

//Returns Member
Member* linked_member(){return this->member;}

private:
Book* book;
Member* member;
};

Association.cpp
#ifndef _ASSOCIATIONCPP
#define _ASSOCIATIONCPP
#include &quot;Association.h&quot;

template<class Book,class Member>
Association<Book,Member>::Association(Book* book, Member* member)
{
this->book=book;
this->member=member;
}
#endif

Apologies for the huge amount of code.
grscot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top