(W) A template dependent name that is a type must be qualified with "typename".
and method definitons give this warning:
(S) The class qualifier "List" contains a circular reference back to ""
How can these be avoided?
Following is a complete demo program, which g++ 4.1.1 accepts without complaint.
Thanks to anyone who can help!
Expand|Select|Wrap|Line Numbers
- template <class T>
- class List {
- public:
- List() : m_Head(0) {};
- void Prepend(T item);
- // 1540-0152 (W) A template dependent name that
- // is a type must be qualified with "typename".
- class Iter;
- friend class List<T>::Iter; // <-- WARNING HERE
- //Does not work: friend class List<typename T>::Iter;
- //Does not work: friend typename class List<T>::Iter;
- //Does not work: friend class typename List<T>::Iter;
- class Iter {
- public:
- Iter(const List & list);
- bool Done(void);
- T GetCurrent(void);
- void Advance(void);
- private:
- typename List<T>::Link * m_Curr;
- };
- private:
- struct Link {
- T value;
- Link *next;
- };
- Link * m_Head;
- };
- // 1540-0100 (S) The class qualifier "List" contains a circular reference back to "".
- template <class T> void List<T>::Prepend(T item) { // <--WARNING HERE
- Link *newlink = new Link;
- newlink->value = item;
- if (m_Head == 0) {
- newlink->next = 0;
- m_Head = newlink;
- } else {
- newlink->next = m_Head;
- m_Head = newlink;
- }
- };
- template<class T> List<T>::Iter::Iter(const List<T> & list)
- : m_Curr(list.m_Head)
- { }
- template<class T> bool List<T>::Iter::Done(void) { return (m_Curr==0); }
- template<class T> T List<T>::Iter::GetCurrent(void) { return m_Curr->value; }
- template<class T> void List<T>::Iter::Advance(void) {
- if (m_Curr != 0) { m_Curr = m_Curr->next; }
- }
- #include <iostream>
- using namespace std;
- int main() {
- typedef List<int> ListOfInt_t;
- ListOfInt_t list;
- for (int i=0; i<10; ++i) { list.Prepend(i); }
- for (ListOfInt_t::Iter iter(list);
- ! iter.Done();
- iter.Advance())
- {
- cout << "Item: " << iter.GetCurrent() << endl;
- }
- return 0;
- }