New compiler chokes on template class

Discussion in 'C++' started by William Payne, Aug 21, 2004.

  1. Hi, I recently upgraded my compiler to a version that's a lot more touchy
    about code not conforming to the ISO standard. Right now I am in the process
    of compiling my old programs with the new compiler. The compiler complains
    about a missing semi-colon in a template class and I don't see the error. I
    must admit I am very weak in the area of templates but I think the program
    compiled with the older version, can't say for sure, though.
    Here's the code:
    #ifndef LIST_HPP
    #define LIST_HPP

    #include <cstddef> /* NULL, size_t */
    #include <stdexcept>

    template<class Type>
    class List
    {
    public:
    List();

    virtual ~List();

    void insert(const Type& T);

    void append(const Type& T);

    void remove_first();

    void remove_last();

    void clear();

    std::size_t size() const
    {
    return m_size;
    }

    bool is_empty() const
    {
    return m_size == 0;
    }

    private:
    class Node;
    public:

    class Iterator
    {
    friend class List<Type>;
    public:
    Iterator()
    :
    m_node(NULL)
    {
    ; /* Do nothing. */
    }

    Iterator(Node* node)
    :
    m_node(node)
    {
    ; /* Do nothing. */
    }

    Iterator& operator++();
    Iterator& operator--();
    Iterator operator++(int);
    Iterator operator--(int);
    const Type& operator*() const;
    bool operator==(const List<Type>::Iterator& rhs) const;
    bool operator!=(const List<Type>::Iterator& rhs) const;

    private:
    Node* m_node;
    };

    List<Type>::Iterator begin() const;

    List<Type>::Iterator end() const;

    private:
    class Node
    {
    friend class List<Type>;
    friend class Iterator;
    public:
    Node()
    :
    m_next(NULL),
    m_previous(NULL)
    {
    ; /* Do nothing. */
    }

    Node(const Type& T)
    :
    Item(T),
    m_next(NULL),
    m_previous(NULL)
    {
    ; /* Do nothing. */
    }

    private:
    Type Item;
    Node* m_next;
    Node* m_previous;
    };

    Node* m_header;
    Node* m_tail;
    std::size_t m_size;
    };

    The compiler chokes on the following line:
    List<Type>::Iterator begin() const;

    the error message is:
    error: expected `;' before "begin"

    What is wrong?

    / WP
     
    William Payne, Aug 21, 2004
    #1
    1. Advertising

  2. William Payne wrote:
    > Hi, I recently upgraded my compiler to a version that's a lot more touchy
    > about code not conforming to the ISO standard. Right now I am in the process
    > of compiling my old programs with the new compiler. The compiler complains
    > about a missing semi-colon in a template class and I don't see the error. I
    > must admit I am very weak in the area of templates but I think the program
    > compiled with the older version, can't say for sure, though.
    > Here's the code:


    .... fun stuff snipped

    When you reference a type in a class that is dependant on the template
    parameter, you need to specify that it is in fact a type by using the
    "typename" keyword.

    e.g.

    >
    > List<Type>::Iterator begin() const;


    should be:

    typename List<Type>::Iterator begin() const;

    >
    > List<Type>::Iterator end() const;


    should be:

    typename List<Type>::Iterator end() const;

    .... more snipola


    >
    > The compiler chokes on the following line:
    > List<Type>::Iterator begin() const;
    >
    > the error message is:
    > error: expected `;' before "begin"
    >
    > What is wrong?


    You must use "typename" as required by the standard.
     
    Gianni Mariani, Aug 21, 2004
    #2
    1. Advertising

  3. "Gianni Mariani" <> wrote in message
    news:cg8449$...
    > William Payne wrote:
    >> Hi, I recently upgraded my compiler to a version that's a lot more touchy
    >> about code not conforming to the ISO standard. Right now I am in the
    >> process of compiling my old programs with the new compiler. The compiler
    >> complains about a missing semi-colon in a template class and I don't see
    >> the error. I must admit I am very weak in the area of templates but I
    >> think the program compiled with the older version, can't say for sure,
    >> though.
    >> Here's the code:

    >
    > ... fun stuff snipped
    >
    > When you reference a type in a class that is dependant on the template
    > parameter, you need to specify that it is in fact a type by using the
    > "typename" keyword.
    >
    > e.g.
    >
    >>
    >> List<Type>::Iterator begin() const;

    >
    > should be:
    >
    > typename List<Type>::Iterator begin() const;
    >
    >>
    >> List<Type>::Iterator end() const;

    >
    > should be:
    >
    > typename List<Type>::Iterator end() const;
    >
    > ... more snipola
    >
    >
    >>
    >> The compiler chokes on the following line:
    >> List<Type>::Iterator begin() const;
    >>
    >> the error message is:
    >> error: expected `;' before "begin"
    >>
    >> What is wrong?

    >
    > You must use "typename" as required by the standard.
    >


    Thank you
     
    William Payne, Aug 21, 2004
    #3
  4. William Payne

    Old Wolf Guest

    "William Payne" <> wrote:

    [lots of stuff snipped below]
    >
    > template<class Type>
    > class List
    > {
    > class Iterator { };
    > List<Type>::Iterator begin() const;
    > List<Type>::Iterator end() const;
    > };
    >
    > The compiler chokes on the following line:
    > List<Type>::Iterator begin() const;


    Since Iterator is a member of the class you're currently defining,
    I don't think you need to qualify it:

    Iterator begin() const;

    But FWIW, your version compiles fine on g++ 3.3.1. As Gianni
    mentioned, you can use 'typename' to resolve dependent typenames,
    but I don't know if List<Type>::Iterator counts (it doesn't really
    depend on any information that the compiler doesn't have).
     
    Old Wolf, Aug 22, 2004
    #4
    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. christopher diggins
    Replies:
    16
    Views:
    792
    Pete Becker
    May 4, 2005
  2. ‘5ÛHH575-UAZWKVVP-7H2H48V3
    Replies:
    7
    Views:
    710
    Kanenas
    Feb 15, 2005
  3. Bram Stolk
    Replies:
    4
    Views:
    364
    Bram Stolk
    May 25, 2005
  4. Rene Pijlman
    Replies:
    6
    Views:
    707
    Fredrik Lundh
    May 29, 2006
  5. OtisUsenet
    Replies:
    1
    Views:
    662
    derek
    Sep 19, 2007
Loading...

Share This Page