By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

New compiler chokes on template class

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Jul 22 '05 #2

P: n/a

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cg*******@dispatch.concentric.net...
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
Jul 22 '05 #3

P: n/a
"William Payne" <mi**************@student.liu.se> 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).
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.