[prog] C++ Templates

Dan Richter daniel.richter at wimba.com
Thu Jun 26 17:06:11 EST 2003


>#include <iostream>
>
>template <typename T>
>class Ordlist
>{
<snip>
>};      // class

All this looks okay to me.

>template <typename T>
>Ordlist<T size> : count(size)

I think what you want here is:
   template <typename T>
   Ordlist<T>::Ordlist(int size) : count(size){
   }

The "::" indicates membership. The ":" (which is used only in a 
constructor) is used to call the constructors of class members. This is 
frivolous in this case (you're calling the "constructor" of an int), but 
when you're dealing with complex classes it can be useful.

>        info = new T[size * sizeof(T)];

You don't have to multiply by sizeof(T) here: new T[n] allocates n T's, not 
n bytes.

>template <typename T>
>int insert(T item)

You want:
   template <typename T>
   Ordlist<T>::insert(T item)


>template <>
>int isfull(void)

Nothing in C++ would surprise me, so the above might somehow compile under 
some circumstances, but it's not what you want. You want:
   template <typename T>
   Ordlist<T>::isfull(void)

>void display ( void)

This should be declared similarly to the above methods.

--
  The first time the [artificially intelligent] creature was put down
  in the game world, he just stared at his feet. I was puzzled, but
  after debugging found that the creature was trying to eat himself.
  He was hungry, and had spotted himself as a nearby convenient object!
      - Richard Evans, creator of the AI game "Black & White"



More information about the Programming mailing list