473,486 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using iterators inside templates

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
2 1392
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
4218
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
3
2453
by: Old Wolf | last post by:
Hi all. G++ fails to compile the following: #include <string> int main() { std::string foo("abc=123"); std::string::const_iterator delimiter = std::find(foo.begin(), foo.end(), '=');
1
1874
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
0
312
by: D.C.Dunn | last post by:
Please forgive the naivety of my following question. I'm new to C++, and am trying to get to grips with OOP. My question concerns iterators and derived classes. Please note that I don't want to use...
24
3910
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
19
7470
by: John | last post by:
In STL's map implementation, the distance between two iterators it1, it2 takes O(K) to compute where K is the actual distance between the two iterators. In theory, a red black tree can do this...
6
2917
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
8
1575
by: pvonnied | last post by:
Hi, Once more a question (sorry for the different e-mail addresses , here @ work we have to use Google to post to newsgroups): If I use an iterator, say from std::map, I initialize it like so:...
6
1837
by: Rennie deGraaf | last post by:
Hello, I would like to write a function that reads a sequence of unsigned shorts from /any/ container, converts them to pairs of unsigned chars, and writes them to /any/ container. In other...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.