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

Help with Iterator for templated list.

P: n/a
Hi,
I'm trying to iterate over the values of a list that has a
templated type and I'm having trouble with the syntax, could anyone
offer somehelp. Simplified version...

#include <list>
#include <iterator>
using namspace std;

template <class T>
class test {
public:
void add(T a) { l.push.back(a); }
void print(void) {
list<T>::iterator itr; // <-- compile error here

// ... do print thang
}

private:
list<Tl;
}

int main(void) {
test<floata;

a.add(5.0);
}
Compilation fails in the list iterator declaration with:
In member function 'void test<T>::print(): error: expected ';' before
itr.

I can't find a list example using iterators and templates for the life
of me so I'm hoping someone can help me with some n00b syntax =).

Nov 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Use typename to say compiler that list<T>::iterator is a name of type.

th********@gmail.com wrote:
Hi,
I'm trying to iterate over the values of a list that has a
templated type and I'm having trouble with the syntax, could anyone
offer somehelp. Simplified version...

#include <list>
#include <iterator>
using namspace std;

template <class T>
class test {
public:
void add(T a) { l.push.back(a); }
void print(void) {
typename list<T>::iterator itr; // <-- compile error here
>
// ... do print thang
}

private:
list<Tl;
}

int main(void) {
test<floata;

a.add(5.0);
}
Compilation fails in the list iterator declaration with:
In member function 'void test<T>::print(): error: expected ';' before
itr.

I can't find a list example using iterators and templates for the life
of me so I'm hoping someone can help me with some n00b syntax =).
Nov 2 '06 #2

P: n/a
So simple...thanks =).
Shelyag Yuriy wrote:
Use typename to say compiler that list<T>::iterator is a name of type.

th********@gmail.com wrote:
Hi,
I'm trying to iterate over the values of a list that has a
templated type and I'm having trouble with the syntax, could anyone
offer somehelp. Simplified version...

#include <list>
#include <iterator>
using namspace std;

template <class T>
class test {
public:
void add(T a) { l.push.back(a); }
void print(void) {
typename list<T>::iterator itr; // <-- compile error here

// ... do print thang
}

private:
list<Tl;
}

int main(void) {
test<floata;

a.add(5.0);
}
Compilation fails in the list iterator declaration with:
In member function 'void test<T>::print(): error: expected ';' before
itr.

I can't find a list example using iterators and templates for the life
of me so I'm hoping someone can help me with some n00b syntax =).
Nov 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.