By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

Using iterators inside templates

P: n/a
I have looked all afternoon and I can't find a definite answer on the
following problem. I have defined a class
My question is why can't I define my iterator below?

template<class T>
class myClass {
public:
void myfunction();
};

and then a function:

template<class T>
void myclass<T>::myfunction() {
multimap<double, T* > i; // why is this allowed and
multimap<double, T* >::iterator j; // why is this not allowed?
}

The error I get with the iterator line is "parse error before '=' token".

I read somewhere that you could not define partial iterators - but if you
cannot use them why bother being able to define containers in template
classes?

Any suggestions, workarounds, reasons would be greatly appreciated.

Many thanks

Tom
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tom McCallum wrote:
I have looked all afternoon and I can't find a definite answer on the
following problem. I have defined a class
My question is why can't I define my iterator below?

template<class T>
class myClass {
public:
void myfunction();
};

and then a function:

template<class T>
void myclass<T>::myfunction() {
multimap<double, T* > i; // why is this allowed and
multimap<double, T* >::iterator j; // why is this not allowed?
Anything after '::' is a "dependent name". You have to tell the compiler
what you mean by using it.

Try

typename std::multimap<double,T*>::iterator j;

since you intend to use 'iterator' as a name of a type.
}

The error I get with the iterator line is "parse error before '=' token".

I read somewhere that you could not define partial iterators - but if
you cannot use them why bother being able to define containers in
template classes?

Any suggestions, workarounds, reasons would be greatly appreciated.


HTH

Victor
Jul 22 '05 #2

P: n/a
On Mon, 24 May 2004 20:11:25 +0100, Tom McCallum
<te********@hotmail.com> wrote:
I have looked all afternoon and I can't find a definite answer on the
following problem. I have defined a class
My question is why can't I define my iterator below?

template<class T>
class myClass {
public:
void myfunction();
};

and then a function:

template<class T>
void myclass<T>::myfunction() {
multimap<double, T* > i; // why is this allowed and
multimap<double, T* >::iterator j; // why is this not allowed?
}


In addition to Victor's comments, you may want to reconsider your use
of multimap<double, ...>, since this may assert:

typedef multimap<double, int> m_t;
m_t m;
m.insert(m_t::value_type(2.3, 0));
assert(m.count(2.3));

Don't forget that doubles are only approximate.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.