Thread: Templates in C++

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Templates in C++

    Hi everyone,
    as the title states I have a question about templates definitions. I have defined a Stack<T> class and a BinaryTree<T> class and both work fine, in fact I can have stacks (or trees) of integers, doubles or any other object. However, if I declare a stack of trees of pointer to characters as follows:
    Code:
    Stack<BinaryTree<char*>> stack;
    I get a compiling error regarding the templates definition. I thought this worked as for Java, where that statement would be fine since the tree and the stack classes supported generic objects, but I suppose I had it wrong! What am I missing?.. Am I obliged to define the stack template differently?

    Cheers

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by flexo87 View Post
    Code:
    Stack<BinaryTree<char*>> stack;
    Put a space amidst >>.
    (This bug is not there for C++11 supporting compilers.)

    Also... there may be other problems if you relied upon implicit comparator functions or operators and didn't provide those for the present 'T' .

  3. #3
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Quote Originally Posted by manasij7479 View Post
    Also... there may be other problems if you relied upon implicit comparator functions or operators and didn't provide those for the present 'T' .
    Thanks, I will take that into account!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    (This bug is not there for C++11 supporting compilers.)
    This is not a bug. Rather, it is a missing feature. The standard does not specify that compilers must interpret >> differently depending on context to simplify compilers.
    It is now standardized in C++11 that >> will be interpreted differently depending on context (for example, shifting a number of a template declaration).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with templates
    By hanniball in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 06:16 PM
  2. About Templates
    By mike_g in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2007, 02:56 AM
  3. Templates
    By creativeinspira in forum C++ Programming
    Replies: 7
    Last Post: 08-07-2007, 11:04 PM
  4. using templates
    By Benzakhar in forum Windows Programming
    Replies: 9
    Last Post: 11-24-2003, 06:53 PM
  5. Templates ........
    By Moni in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2002, 04:34 PM

Tags for this Thread