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

W

weatherman

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;
}
 
K

Kai-Uwe Bux

weatherman said:
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 said:
public:

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

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

typename list said:
{ 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top