class definition in template class

M

Marcosu

Hi,

I have a problem in a template with a class definition.
In a template class (Foo) ther is a class (Bar) and a list
of pointer to this class (m_l). In Foo's destructor I would like
to clear the list but I can't define the iterator to scan the list.

The reducted code is:

# include <list>

template <class T>
class Foo {
class Bar {
public:
int i;
};

std::list<Bar*> m_l;
T m_value;
// other stuff

~Foo() {
// gcc 4.1.1 error: expected `;' before 'i'
std::list<Bar*>::iterator i;

for (i = m_l.begin(); i != m_l.end(); i++) {
delete *i;
}
}
};

Changing the Bar* argument to char* in the line with problem all works
well,
but is not what I want!

What I need to correct the iterator definition?

Thanks in advance,
Marco
 
B

benben

Marcosu wrote:
[snip]
# include <list>

template <class T>
class Foo {
class Bar {
public:
int i;
};

std::list<Bar*> m_l;
T m_value;
// other stuff

~Foo() {
// gcc 4.1.1 error: expected `;' before 'i'
std::list<Bar*>::iterator i;

for (i = m_l.begin(); i != m_l.end(); i++) {
delete *i;
}
}
};
[snip]

Declare it like this:

typename std::list<Bar*>::iterator i;

Also, I guess you didn't really mean to make the destructor private, right?
Thanks in advance,
Marco

Ben
 
M

Marcosu

benben ha scritto:
Marcosu wrote:
[snip]
# include <list>

template <class T>
class Foo {
class Bar {
public:
int i;
};

std::list<Bar*> m_l;
T m_value;
// other stuff

~Foo() {
// gcc 4.1.1 error: expected `;' before 'i'
std::list<Bar*>::iterator i;

for (i = m_l.begin(); i != m_l.end(); i++) {
delete *i;
}
}
};
[snip]

Declare it like this:

typename std::list<Bar*>::iterator i;

Ok, works well.
Also, I guess you didn't really mean to make the destructor private, right?

Yes! it's a Foo definition!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top