Re: STL container iterator not available within template?

Discussion in 'C++' started by Saeed Amrollahi, Jun 18, 2009.

  1. On Jun 18, 1:29 pm, "Sam" <> wrote:
    > Hi there,
    >
    > Here's the problem I just encountered. For the following code
    >
    > #include <set>
    >
    > template <class T>
    > class A
    > {
    >     std::set<T*>::iterator a;
    >
    > };
    >
    > int main()
    > {
    >
    > }
    >
    > g++ generated this error:
    > t.cpp:6: error: expected `;' before "a"
    >
    > Anyone can help? Thanks in advance.
    >
    > Best Regards,
    > Sam


    Hi Sam

    use typename:
    typename std::set<T*>::iterator a;

    Regards,
    -- Saeed Amrollahi
    Saeed Amrollahi, Jun 18, 2009
    #1
    1. Advertising

  2. Sam wrote:
    >> Hi Sam
    >>
    >> use typename:
    >> typename std::set<T*>::iterator a;
    >>
    >> Regards,
    >> -- Saeed Amrollahi

    >
    > Thank you very much Saeed! Problem solved!


    Basically the idea is that since T is a template parameter type, the
    std::set<T*>::iterator type becomes dependent of T. The standard
    requires that whether that "iterator" is a type or not must be
    disambiguated with the typename keyword. Without the typename keyword
    the compiler assumes that "iterator" is actually a member of
    std::set<T*>, rather than a nested type.

    (Technically speaking, the compiler could *assume* it's a type because
    it's being used like a type, and in fact older compilers did just that.
    However, the standard requires the disambiguation.)
    Juha Nieminen, Jun 18, 2009
    #2
    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. Vivi Orunitia
    Replies:
    11
    Views:
    4,450
    Martijn Lievaart
    Feb 4, 2004
  2. forester
    Replies:
    16
    Views:
    708
    Howard Hinnant
    Aug 25, 2005
  3. Replies:
    4
    Views:
    786
    Daniel T.
    Feb 16, 2006
  4. Christoph  Heindl

    STL inherit from container<T>::iterator

    Christoph Heindl, Apr 3, 2006, in forum: C++
    Replies:
    2
    Views:
    563
    Christoph Heindl
    Apr 4, 2006
  5. gallows
    Replies:
    3
    Views:
    796
    gallows
    Mar 5, 2007
Loading...

Share This Page