Porting from g++ 2.95/3.2 to g++ 4.01 template problem

Discussion in 'C++' started by weatherman, Sep 15, 2005.

  1. weatherman

    weatherman Guest

    Dear all,

    When porting some code from g++ 2.95/3.2 to g++ 4.01 we
    encountered some difficulties with the following construction.
    The code is known to compile on g++ 2.95 and g++ 3.2.2.

    This is a condensed test case based on our code.
    We believe this is a general c++ standard compliance problem,
    and not a g++ problem or bug.

    However, we have been unable to find out how this should be done!
    The compilation stops at the definition of current_item_,
    the compiler does not manage to resolve that line.

    (Ideally the code is going to be used to make a slightly specialized
    list class from the standard c++ list class)

    If anyone can point us in the right directions, we would be
    immensely grateful.

    - Gunnar
    ----------------------------------------------------------
    MeListTest.h:
    ----------------------------------------------------------
    #ifndef MELISTTEST_H
    #define MELISTTEST_H

    #include <list>

    using namespace std;

    template <typename Item>
    class ListTest : public list<Item>
    {
    protected:
    list<Item>::iterator current_item_;

    public:

    ListTest() {}
    ListTest(const ListTest& src) : list<Item>(src),
    current_item_(src.current_item_) {}
    virtual ~ListTest() {}

    list<Item>::iterator getIterator(int index)
    { return current_item; }
    };

    #endif

    -----------------------------------------------------
    main.cpp
    -----------------------------------------------------

    #include "MeListTest.h"

    int main()
    {
    ListTest<int> mylist;
    }
     
    weatherman, Sep 15, 2005
    #1
    1. Advertising

  2. weatherman

    Kai-Uwe Bux Guest

    weatherman wrote:

    > Dear all,
    >
    > When porting some code from g++ 2.95/3.2 to g++ 4.01 we
    > encountered some difficulties with the following construction.
    > The code is known to compile on g++ 2.95 and g++ 3.2.2.
    >
    > This is a condensed test case based on our code.
    > We believe this is a general c++ standard compliance problem,
    > and not a g++ problem or bug.
    >
    > However, we have been unable to find out how this should be done!
    > The compilation stops at the definition of current_item_,
    > the compiler does not manage to resolve that line.
    >
    > (Ideally the code is going to be used to make a slightly specialized
    > list class from the standard c++ list class)
    >
    > If anyone can point us in the right directions, we would be
    > immensely grateful.
    >
    > - Gunnar
    > ----------------------------------------------------------
    > MeListTest.h:
    > ----------------------------------------------------------
    > #ifndef MELISTTEST_H
    > #define MELISTTEST_H
    >
    > #include <list>
    >
    > using namespace std;


    bad idea in headers.

    >
    > template <typename Item>
    > class ListTest : public list<Item>
    > {
    > protected:
    > list<Item>::iterator current_item_;


    typename list<Item>::iterator current_item_;
    >
    > public:
    >
    > ListTest() {}
    > ListTest(const ListTest& src) : list<Item>(src),
    > current_item_(src.current_item_) {}
    > virtual ~ListTest() {}
    >
    > list<Item>::iterator getIterator(int index)


    typename list<Item>::iterator getIterator(int index)

    > { return current_item; }


    { return current_item_; }

    > };
    >
    > #endif
    >
    > -----------------------------------------------------
    > main.cpp
    > -----------------------------------------------------
    >
    > #include "MeListTest.h"
    >
    > int main()
    > {
    > ListTest<int> mylist;
    > }



    Within templates, the compiler does not make assumptions about what kind of
    entity an identifier like list<Item>::iterator refers to. This can only be
    deduced once Item is known. Thus, you have to tell the compiler that
    iterator is a type and not a member function or member object.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 15, 2005
    #2
    1. Advertising

  3. weatherman

    weatherman Guest

    Thanks. The medicine works!

    -- gunnar
     
    weatherman, Sep 15, 2005
    #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. Chris Theis
    Replies:
    2
    Views:
    502
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    567
    tom_usenet
    Jul 24, 2003
  3. Michele
    Replies:
    3
    Views:
    525
    Glen Low
    Oct 6, 2003
  4. Chuck Chopp
    Replies:
    4
    Views:
    416
    Chuck Chopp
    Aug 25, 2006
  5. Aadain
    Replies:
    6
    Views:
    520
    Arved Sandstrom
    May 19, 2008
Loading...

Share This Page