New compiler chokes on template class

W

William Payne

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
 
G

Gianni Mariani

William said:
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 said:
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.
 
W

William Payne

Gianni Mariani said:
... 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.


should be:



should be:

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

... more snipola



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

Thank you
 
O

Old Wolf

[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).
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top