0

I am reading the book Thinking in C++ (2nd edition Vol 1) and i have a problem when i try to compile this source code:

http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter16_015.html

The problem appears to be in that line:

Stack::Link* p;

As i have read in C++: error "... is not derived from type ..." i have to add the word "typename" (or "class") in front of it.

Is the book wrong in this example or am I missing something?

Community
  • 1
  • 1
nikitas350
  • 57
  • 4

4 Answers4

3

Yes, it's wrong. You need the typename. See the typename and template FAQ entry.

Stack is a dependent type and Stack::List is a dependent type too. This is legal in C++0x, but it's illegal in current C++.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • and what about in C++98, the version that was current when the book was released? – Ben Voigt Dec 12 '10 at 00:26
  • @Ben It was needed there aswell. The only change I remember offhead introduced to C++03 that's remotely relevant here is that the *unqualified* name `List` was made a dependent type. But that of course is a different matter. – Johannes Schaub - litb Dec 12 '10 at 00:27
0

Is the book wrong in this example or am I missing something?

I've found that different compilers have different requirements for the typename keyword. MSVC is fairly forgiving, gpp is more strict.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
0

It's more a case of you using too old a book. The publication date of that book is 2000, meaning much of the actual writing was taking place when the C++98 standard was being finalized.

The current version of C++ is C++03, and C++0x is fast approaching standardization.

I don't think that typename was required in C++98.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

This may be too late in day but the code should compile if you change

Stack::Link* p;

to just

Link* p;

John Gathogo
  • 4,495
  • 3
  • 32
  • 48