473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

New compiler chokes on template class

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
3 1233
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and...
5
by: Shlomi | last post by:
Hi ! I've wrote quite a big code which bases on a considerable large amount of template classes. (uses many different kinds of each template class). Now, without prior knowledge of mine, it...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
13
by: Eric | last post by:
I have a template class that instantiates a variable of the template parameter type as follows: template <class T> struct TemplateClass { T t; }; struct TestClass
5
by: Lou Pecora | last post by:
g++ compiler error question. I have a container C whose constructor takes a class B that is inherited from an abstract class A. So I have the line of code: B binstance; C ...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
3
by: Benedikt Weber | last post by:
I found the following compiler error with Microsoft Visual C++ .NET. I use different functions with return types determined by a Traits class. Function g() below works ok, but when I put the two...
5
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.