class definition in template class

Discussion in 'C++' started by Marcosu, Aug 23, 2006.

  1. Marcosu

    Marcosu Guest

    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
     
    Marcosu, Aug 23, 2006
    #1
    1. Advertising

  2. Marcosu

    benben Guest

    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
     
    benben, Aug 23, 2006
    #2
    1. Advertising

  3. Marcosu

    Marcosu Guest

    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
     
    Marcosu, Aug 23, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    597
    Victor Bazarov
    Mar 13, 2005
  2. BigMan
    Replies:
    3
    Views:
    1,438
    BigMan
    Apr 22, 2005
  3. =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Replies:
    5
    Views:
    2,333
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Dec 14, 2006
  4. Pierre Yves
    Replies:
    2
    Views:
    494
    Pierre Yves
    Jan 10, 2008
  5. A L
    Replies:
    1
    Views:
    512
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page