Sign in to follow this  

Template defintion trouble

This topic is 3589 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 usually try and keep my function definitions seperate from the declarations (aka not just put them together). I've got some code I'm trying to store an iterator from any stl-compatible container type.
//  Declaration
template <typename T, template <typename> class U>
class MyClass
{
public:
	MyClass(typename U<T>::iterator itr);
	MyClass(const MyClass& rhs);
	~MyClass();
	MyClass& operator = (const MyClass& rhs);

	T& operator * ();

private:
	typename U<T>::iterator	m_itr;
};



template <typename T, template <typename> class U>
MyClass<T, U>::MyClass(typename U<T>::iterator itr) : m_itr(itr)
{
}



I'm not sure if these can actually be seperate or not, or if the definition just needs to be embedded. Would "MyClass<T, U>::" be correct, or does it need to be a variation on that since U = "template <typename> class U"? Also the compiling error I'm getting from VS 2005 is error C2244: 'MyClass<T, U>::{ctor}' : unable to match function definition to an existing declaration definition 'MyClass<T, U>::MyClass(U<T>::iterator)' existing declarations 'MyClass<T, U>::MyClass(const MyClass<T,U> &)' 'MyClass<T, U>::MyClass(U<T>::iterator)' So the two existing ones are the constructor and the copy-constructor. Strangely enough, it's listing an exact match for the constructor (as far as what it says it can't match). Any ideas? [edit] Fixed the source tags and some <> that got messed up

Share this post


Link to post
Share on other sites
MinGW 3.4.2 and Borland 5.82 both grok your code just fine. Visual C++ 2008 gives the error you listed. Making a slight change:
template <typename T, template <typename> class U>
class MyClass
{
public:
typedef typename U<T>::iterator iterator
MyClass(iterator itr);
MyClass(const MyClass& rhs);
~MyClass();
MyClass& operator = (const MyClass& rhs);

T& operator * ();

private:
typename U<T>::iterator m_itr;
};

template <typename T, template <typename> class U>
MyClass<T, U>::MyClass(typename MyClass<T, U>::iterator itr) : m_itr(itr)
{
}
is sufficient to get it to work under Visual C++ 2008 as well. So it looks like either a really obscure template issue or a compiler bug.

Σnigma

Share this post


Link to post
Share on other sites

This topic is 3589 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