Sign in to follow this  

a STL question?

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just write a member function for my template class LinkedList<ElementType*>, within which a std::list<ElementType*> is its data member. And I also make this wrapped function InsertBefore, like this
	ElementType* InsertBefore(std::list<ElementType*>::iterator nextElement,
		ElementType* newElement)
	{
		ElementType* pNewElement = new ElementType(*newElement);
		std::list<ElementType*>::iterator iter = m_list.begin();

		m_list.insert(iter, pNewElement);

		return pNewElement;
	}
	

Unfortunately, it cannot work. My VC just indicate that warning C4346: 'std::list<ElementType*>::iterator' : dependent name is not a type prefix with 'typename' to indicate a type error C2061: syntax error : identifier 'iterator' which to me I think is a little bizarre. Isn't the std::list<T>::insert implemented in the same way? Could anyone explain this to me please:)

Share this post


Link to post
Share on other sites
Your warning explains pretty clearly what has to be done:

warning C4346: 'std::list<ElementType*>::iterator' : dependent name is not a type
prefix with 'typename' to indicate a type


Result:

typedef std::list<ElementType*> list_type;
typedef typename MyList::iterator iterator_type;

ElementType* InsertBefore(iterator_type nextElement, ElementType* newElement)
{
ElementType* pNewElement = new ElementType(*newElement);
iterator_type iter = m_list.begin();

m_list.insert(iter, pNewElement);

return pNewElement;
}


Share this post


Link to post
Share on other sites

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this