Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > New compiler chokes on template class

Reply
Thread Tools

New compiler chokes on template class

 
 
William Payne
Guest
Posts: n/a
 
      08-21-2004
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


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-21-2004
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.

 
Reply With Quote
 
 
 
 
William Payne
Guest
Posts: n/a
 
      08-21-2004

"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:cg8449$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-22-2004
"William Payne" <(E-Mail Removed)> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
IRIX MipsPro compiler chokes on Python.h Bram Stolk Python 4 05-25-2005 04:15 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Explorer.exe chokes CPU 100% during 2 mins from startup Maria Computer Support 3 04-17-2005 04:07 PM
Norton Antispam Chokes Outlook Express SNTP Computer Support 5 02-10-2004 04:21 AM



Advertisments