Nested template class doesn't compile with VC++ 2003

Z

zhi.wong

The following code is a simplification of a example in "The C++
programming language". So, it is assumed to be STANDARD c++. But Visual
C++ 2003 complains about the function defination of get_free. Is M$
VC2003 reaaly standard in this regard?

template<class T> class MyList {
private:
struct Link {
T val;
Link *next;
};

Link* free;
Link* get_free ();
public:
void insert (T);
}

template<class T> void MyList<T>::insert (T value)
{
return;
}

template<class T>MyList<T>::Link *MyList<T>::get_free ()
{
return NULL;
}
 
T

Tom Widmer

The following code is a simplification of a example in "The C++
programming language". So, it is assumed to be STANDARD c++. But Visual
C++ 2003 complains about the function defination of get_free. Is M$
VC2003 reaaly standard in this regard?

template<class T> class MyList {
private:
struct Link {
T val;
Link *next;
};

Link* free;
Link* get_free ();
public:
void insert (T);
}

template<class T> void MyList<T>::insert (T value)
{
return;
}

template<class T>MyList<T>::Link *MyList<T>::get_free ()

That should be:

template<class T>
typename MyList<T>::Link *MyList<T>::get_free ()

Tom
 
Z

zhi.wong

Yes, a typename helps it.
But I think its redunant here, the compiler could have figured
it out that Link is a type name.

Thanks for helping
 
V

Victor Bazarov

Yes, a typename helps it.
But I think its redunant here, the compiler could have figured
it out that Link is a type name.

(a) Please don't top-post.

(b) How could the compiler have figured it out? Don't rush with
the answer, imagine that I've defined a specialisation of your
template 'MyList' such that 'Link' is not a type any longer.
The compiler cannot automatically assume that the first thing
it sees is a type.
 
Z

zhi.wong

Victor said:
(a) Please don't top-post.
OK

(b) How could the compiler have figured it out? Don't rush with
the answer, imagine that I've defined a specialisation of your
template 'MyList' such that 'Link' is not a type any longer.
The compiler cannot automatically assume that the first thing
it sees is a type.

You are right, g++ also complains about deprecated typename
when given the code fragment. So, the book is a little out of date and
impractical about this template thing? or, I just grabbed the wrong
book for a beginner.
 
T

Tom Widmer

You are right, g++ also complains about deprecated typename
when given the code fragment. So, the book is a little out of date and
impractical about this template thing? or, I just grabbed the wrong
book for a beginner.

Which book is it? Certainly, Stroustrup's book is about the standard
language, so if it had the code below, its an error in the book.

A good beginners book is apparently "Accelerated C++".

Tom
 
Z

zhi.wong

Tom said:
Which book is it? Certainly, Stroustrup's book is about the standard
language, so if it had the code below, its an error in the book.

It's "The C++ programming language, Special 3rd edition". The code
is from page 403, $15.3 access control. I changed it a little to
illustrate the problem concisely. Have I made a mistake during that?
 
V

Victor Bazarov

It's "The C++ programming language, Special 3rd edition". The code
is from page 403, $15.3 access control. I changed it a little to
illustrate the problem concisely. Have I made a mistake during that?

While TC++PL is a very good book, it just doesn't have enough room to
contain all possible things about all aspects of the language. It is
also conceivable that it contains errors or out-of-date elements. To
get the latest and most comprehensive template text, get a copy of
"C++ Templates" by Vandevoorde and Josuttis.

V
 

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

Forum statistics

Threads
474,186
Messages
2,570,997
Members
47,586
Latest member
Gilda57E93

Latest Threads

Top