Need help for passing template class as reference using gcc...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone,


I have encountered a problem compiling template classes using gcc
version 4.0.3. The code compiles and works fine under Visual Studio
8.0.

Below is a sample of the code that causes the issues. The
AVLTree::begin () function returns an Iterator by value. This Iterator
is created automatically by calling the constructor
"Iterator::Iterator (item_type* node_ptr);" below when I return the
item_type* pointer. Then, I would like to pass this newly created
object by reference by calling the "Iterator::Iterator <item_type>&
operator=(Iterator <item_type>& iter);" function. However, it seems
that gcc cannot pass the newly created Iterator object as reference.
At first I thought that I should overload the & operator for the
Iterator class but it didn't help.

I think the problem could be resolved by passing the Iterator from
AVLTree::begin () by reference (although I haven't tried) but in that
case I would have to worry about allocating/deallocating memory and
this would make my code less robust. Is there any way to tell the
compiler how to automatically convert

Iterator<item_type>

to

Iterator<item_type>&

with item_type

AVLTree<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, Task*>

?

The error message:

../../core/Task.cpp: In member function `virtual void
Task::visitor_update_task_avltree(TASK_AVLTREE*)':
../../core/Task.cpp:1787: error: no match for `operator=' in
`task_iter = AVLTree<key_type, item_type>::begin() [with key_type =
std::basic_string<char, std::char_traits<char>, std::allocator<char>
, item_type = Task*]()'
../../core/../datastructures/Iterator.cpp:104: note: candidates are:
Iterator<item_type>&
Iterator<item_type>::operator=(Iterator<item_type>&) [with item_type =
AVLTreeNode<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, Task*>]
../../core/../datastructures/Iterator.cpp:111: note:
Iterator<item_type>&
Iterator<item_type>::operator=(Const_Iterator<item_type>&) [with
item_type = AVLTreeNode<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, Task*>]

Code:

template <class item_type>
class Iterator
{
public:

	Iterator () {};

	Iterator (item_type* node_ptr);

	Iterator (Iterator <item_type>& iter);

...

	Iterator <item_type>& operator=(Iterator <item_type>& iter);

	Iterator <item_type>& operator=(Const_Iterator <item_type>& iter);

...

	Iterator <item_type>& operator& ();

...

};


template <class key_type, class item_type>
class AVLTree
{

	AVLTree ();

...

	Iterator <AVLTreeNode <key_type, item_type> > begin ();

...

};


void Task::visitor_update_task_avltree (TASK_AVLTREE* task_avltree)
	throw (Exception)
{
	TASK_AVLTREE::iterator task_iter;

	for (task_iter = task_avltree -> begin (); task_iter != task_avltree
-> end (); ++task_iter)
	{

		...

	}
}

Thanks,
Gabe

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux