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 again

Status
Not open for further replies.

grscot

Programmer
Apr 25, 2003
16
GR
Dear all,

The only reason I posted the whole code was to give one the chance to run the program and see in practice the problem.
But I guess you are right, that is too much.
Anyway,
The implementation of the two functions is:
Code:
//It returns the 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;
}

Code:
//it returns the book
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;
}

The template file that I use to display the books is:
Code:
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 it does not work for Members. I've tried to use the recommendations you've told me but it seems not to work.


The program works ok, when I call the functions:
book1.addElement(&quot;Books&quot;); and book1.displayElement(&quot;Books&quot;);
to add a book and to display them respectively.

But when I use the :
member1.addElement(&quot;Member&quot;); and member1.displayElement(&quot;Member&quot;); it dispalys the problem:

The compiler complains at the point:
this->element_list[element]->display(association_list.get_member(element_list[element])); saying that : get_member: cannot convert parameter one from Class Member* to class Book*.


Can you correct any mistakes or suggest any solutions?
Best Regards,
grscot
 
Posting the same question five times in one day isn't helping anything. Be patient.

Please read my response to your last post.


Why do you insist on creating a function template for these two member functions? Is there some reason? Are you just experimenting?

If you have no reason, I suggest you not use a function template and implement the functions separately.
 
Instead of having two functions, to display the members from the member array and the books from the book array, I have to use one template function to perform both operations(if this is possible). I don't know if this is possible, so that is why I'm posting my problems.

Regards,
grscot
 
It's possible.
It's not pretty.
Like I mentioned earlier, as far as I can tell, it would require some advanced template metaprogramming tricks.


If you really have to do it with one function template, my suggestion is as follows:

Go to the bookstore.
Get Modern C++ Design by Andrei Alexandrescu.
Read chapter 3, Typelists.
Pay special attention to the example in section 3.13.3, &quot;Generating Scattered Heirarchies.&quot;
Download the latest version of the Loki library (which the book is about).
Use Loki to make your Association class look more like the example in section 3.13.3 (the one involving the Holder template).
Change the AssociationList to access the Association objects accordingly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top