Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > class definition in template class

Reply
Thread Tools

class definition in template class

 
 
Marcosu
Guest
Posts: n/a
 
      08-23-2006
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

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      08-23-2006
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
 
Reply With Quote
 
 
 
 
Marcosu
Guest
Posts: n/a
 
      08-23-2006

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!

> >
> > Thanks in advance,
> > Marco
> >

>
> Ben


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
Template member functions in template class with separate definition =?iso-8859-1?q?Erik_Wikstr=F6m?= C++ 5 12-14-2006 01:04 PM
Out-of-class definition of a non-template member of an explicitly specialized class template? BigMan C++ 3 04-22-2005 06:28 AM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM



Advertisments