Archived

This topic is now archived and is closed to further replies.

Eriond

STL distance() error

Recommended Posts

#include <iostream>
#include <set>

template<typename T> inline void PrintElements(const T &coll, const char *str = "" )
{
	typename T::const_iterator pos;
	std::cout << str;
	for (pos = coll.begin(); pos != coll.end(); ++pos)
		std::cout << *pos << " ";
	std::cout << std::endl;
}

int main(int argc, char *argv[])
{
	using namespace std;
	set<int, greater<int> > coll1;
	coll1.insert(4);
	coll1.insert(3);
	coll1.insert(5);
	coll1.insert(1);
	coll1.insert(6);
	coll1.insert(2);
	coll1.insert(5);
	PrintElements(coll1, "initialized: ");

	pair<set<int, greater<int> >, bool> status = coll1.insert(4);
	if (status.second)
	{
		cout << "4 inserted as element " << distance(coll1.begin(), status.first) + 1 << endl;
	}
	else
	{
		cout << "4 already exists" << endl;
	}
	return 0;
}
The error I get is: error C2782: 'iterator_traits<_InIt>::difference_type std::distance(_InIt,_InIt)' : template parameter '_InIt' is ambiguous. The example is from The C++ Standar Library: A Tutorial and Reference, by N. Josuttis. I'm using VC++.NET 2003 if that matters. Is there any problems with the compiler or a typo in the book or (most likely) am I doing something wrong? EDIT: Color error....How do you fix that? [edited by - Eriond on January 5, 2004 11:18:02 AM]

Share this post


Link to post
Share on other sites
You mis-entered the type for status. It should be a std::pair< std::set<int, std::greater<int > >::iterator, bool >

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Dude, use typedefs! Nobody wants to look at stuff like

pair<set<int, greater<int> >, bool>

when it could (should) be just

pair<Foo, bool>

Share this post


Link to post
Share on other sites