Instanciating template classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ernesto

    Instanciating template classes

    Hola gurús:

    I'm programming my own collection classes and I already implemented a
    DynamicArray defined as:

    template <class T>
    class DynamicArray
    {
    ...
    };

    Now, I want to implement a hash table as follows:

    template <class KEY_T, class VALUE_T>
    class HashTable
    {
    };

    In my hash table, the data will be stored in a DynamicArray that will
    contain DynamicArrays containing the respective [key;value] objects.
    So, I defined too:

    template <class KEY_T, class VALUE_T>
    class HashTableKeyVal ue
    {
    ...
    KEY_T key;
    VALUE_T value;
    };


    so, I want to create an attribute inside my hash table like:

    DynamicArray < HashTableKeyVal ue<KEY_T, VALUE_T> > ** dataarray;

    i.e., I want my dataarray to contain DynamicArrays that contain
    HashTableKeyVal ues<KEY_T, VALUE_T> ... obviously, it does not compile
    because the notation is not correct (the compiler says that it needs a
    real type, not a template).

    How can I implement this stuff? Do you think my logic is right?
    Thanks in advance


    Saludos



    Ernesto
  • Leor Zolman

    #2
    Re: Instanciating template classes

    On 2 Mar 2004 07:00:18 -0800, ebascon@hotmail .com (Ernesto) wrote:
    [color=blue]
    >Hola gurús:
    >
    >I'm programming my own collection classes and I already implemented a
    >DynamicArray defined as:
    >
    >template <class T>
    >class DynamicArray
    >{
    > ...
    >};
    >
    >Now, I want to implement a hash table as follows:
    >
    >template <class KEY_T, class VALUE_T>
    >class HashTable
    >{
    >};
    >
    >In my hash table, the data will be stored in a DynamicArray that will
    >contain DynamicArrays containing the respective [key;value] objects.
    >So, I defined too:
    >
    >template <class KEY_T, class VALUE_T>
    >class HashTableKeyVal ue
    >{
    > ...
    > KEY_T key;
    > VALUE_T value;
    >};
    >
    >
    >so, I want to create an attribute inside my hash table like:
    >
    >DynamicArray < HashTableKeyVal ue<KEY_T, VALUE_T> > ** dataarray;[/color]

    Did you mean for "dataarray" to point to a 1-dimensional (native)
    dynamically-allocated array of DynamicArray objects, or for it to be a
    pointer to an array of pointers to DynamicArray objects? As written,
    you've specified the latter, but your description sounds like you wanted
    the former... (which you'd get by removing one of the *'s)
    [color=blue]
    >
    >i.e., I want my dataarray to contain DynamicArrays that contain
    >HashTableKeyVa lues<KEY_T, VALUE_T> ... obviously, it does not compile
    >because the notation is not correct (the compiler says that it needs a
    >real type, not a template).[/color]

    I'm trying to figure out what exactly you mean by the KEY_T and VALUE_T in
    your definition:

    DynamicArray < HashTableKeyVal ue<KEY_T, VALUE_T> > ** dataarray;

    because if you precede that with something like:


    typedef int KEY_T;
    typedef int VALUE_T;

    it does compile; it is legal. But it is certainly confusing to have those
    names here /and/ use them as template parameter names in the
    HashTableKeyVal ue template as well...


    [color=blue]
    >
    >How can I implement this stuff? Do you think my logic is right?[/color]

    I'm not a hash table expert so I won't critique your implementation
    strategy, but I hope at least what I've pointed out might be useful...
    -leor
    [color=blue]
    >Thanks in advance
    >
    >
    >Saludos
    >
    >
    >
    >Ernesto[/color]

    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html

    Comment

    • Leor Zolman

      #3
      Re: Instanciating template classes

      I just realized that I /think/ you were saying you wanted to define
      dataarray within the HashTable template...the ordering of your post had me
      a bit confused. Well, that works fine. Here's code that compiles ok (just
      don't try to link it):

      template <class T>
      class DynamicArray
      {
      };

      // Now, I want to implement a hash table as follows:

      template <class KEY_T, class VALUE_T>
      class HashTableKeyVal ue
      {
      KEY_T key;
      VALUE_T value;
      };

      template <class KEY_T, class VALUE_T>
      class HashTable
      {
      DynamicArray < HashTableKeyVal ue<KEY_T, VALUE_T> > ** dataarray;

      };


      Leor Zolman
      BD Software
      leor@bdsoft.com
      www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
      C++ users: Download BD Software's free STL Error Message
      Decryptor at www.bdsoft.com/tools/stlfilt.html

      Comment

      • Ernesto

        #4
        Re: Instanciating template classes

        Leor Zolman <leor@bdsoft.co m> wrote in message news:<aaa940t5g 68n9hvguo1s7ta4 nkdbcodej7@4ax. com>...[color=blue]
        > I just realized that I /think/ you were saying you wanted to define
        > dataarray within the HashTable template...the ordering of your post had me
        > a bit confused. Well, that works fine. Here's code that compiles ok (just
        > don't try to link it):
        >
        > template <class T>
        > class DynamicArray
        > {
        > };
        >
        > // Now, I want to implement a hash table as follows:
        >
        > template <class KEY_T, class VALUE_T>
        > class HashTableKeyVal ue
        > {
        > KEY_T key;
        > VALUE_T value;
        > };
        >
        > template <class KEY_T, class VALUE_T>
        > class HashTable
        > {
        > DynamicArray < HashTableKeyVal ue<KEY_T, VALUE_T> > ** dataarray;
        >
        > };
        >[/color]


        Hi Leor Gurú:

        I implemented:

        DynamicArray<Ha shTableKeyValue <KEY_T, VALUE_T>> ** dataarray; //without whitespaces

        and my compiler did not understand it!!!!

        Your code worked perfectly, thanks a lot!!



        Saludos


        Ernesto

        Comment

        • Leor Zolman

          #5
          Re: Instanciating template classes

          On 2 Mar 2004 12:22:48 -0800, ebascon@hotmail .com (Ernesto) wrote:[color=blue]
          >
          >Hi Leor Gurú:[/color]

          Well, I make too many posting mistakes to qualify as a guru, unless there's
          a special category for "Flaky Guru"...[color=blue]
          >
          >I implemented:
          >
          >DynamicArray<H ashTableKeyValu e<KEY_T, VALUE_T>> ** dataarray; //without whitespaces
          >
          >and my compiler did not understand it!!!![/color]

          You mean the old ">>" without any space between them trick? But the
          original code you posted had the space! Just another example of why it is
          best to copy-and-paste actual code into messages???
          [color=blue]
          >
          >Your code worked perfectly, thanks a lot!!
          >[/color]

          No problemo,
          -lior (Spelled in Spanish, sort of)


          Leor Zolman
          BD Software
          leor@bdsoft.com
          www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
          C++ users: Download BD Software's free STL Error Message
          Decryptor at www.bdsoft.com/tools/stlfilt.html

          Comment

          • Ernesto

            #6
            Re: Instanciating template classes

            Leor Zolman <leor@bdsoft.co m> wrote in message news:<3bs9405am g3i4inpr4a3l6kq 51662q97m7@4ax. com>...[color=blue]
            > On 2 Mar 2004 12:22:48 -0800, ebascon@hotmail .com (Ernesto) wrote:[color=green]
            > >
            > >Hi Leor Gurú:[/color]
            >
            > Well, I make too many posting mistakes to qualify as a guru, unless there's
            > a special category for "Flaky Guru"...[/color]
            [color=blue][color=green]
            > >
            > >I implemented:
            > >
            > >DynamicArray<H ashTableKeyValu e<KEY_T, VALUE_T>> ** dataarray; //without whitespaces
            > >
            > >and my compiler did not understand it!!!![/color]
            >
            > You mean the old ">>" without any space between them trick? But the
            > original code you posted had the space! Just another example of why it is
            > best to copy-and-paste actual code into messages???[/color]

            Yes, I added a whitespace to my message but my actual code did not have it.[color=blue]
            >[color=green]
            > >
            > >Your code worked perfectly, thanks a lot!!
            > >[/color]
            >
            > No problemo,[/color]

            (i.e. No hay problema)

            [color=blue]
            > -lior (Spelled in Spanish, sort of)
            >
            >
            > Leor Zolman
            > BD Software
            > leor@bdsoft.com
            > www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
            > C++ users: Download BD Software's free STL Error Message
            > Decryptor at www.bdsoft.com/tools/stlfilt.html[/color]

            saludos


            Ernesto

            Comment

            • Victor Bazarov

              #7
              Re: Instanciating template classes

              "Ernesto" <ebascon@hotmai l.com> wrote...[color=blue]
              > I'm programming my own collection classes and I already implemented a
              > DynamicArray defined as:
              >
              > template <class T>
              > class DynamicArray
              > {
              > ...
              > };
              >
              > Now, I want to implement a hash table as follows:
              >
              > template <class KEY_T, class VALUE_T>
              > class HashTable
              > {
              > };
              >
              > In my hash table, the data will be stored in a DynamicArray that will
              > contain DynamicArrays containing the respective [key;value] objects.
              > So, I defined too:
              >
              > template <class KEY_T, class VALUE_T>
              > class HashTableKeyVal ue
              > {
              > ...
              > KEY_T key;
              > VALUE_T value;
              > };
              >
              >
              > so, I want to create an attribute inside my hash table like:
              >
              > DynamicArray < HashTableKeyVal ue<KEY_T, VALUE_T> > ** dataarray;[/color]

              So is that going to be like this:

              template <class KEY_T, class VALUE_T>
              class HashTable
              {
              DynamicArray<Ha shTableKeyValue <KEY_T, VALUE_T> > **dataarray;
              };

              or did you do it differently? If so, how?
              [color=blue]
              >
              > i.e., I want my dataarray to contain DynamicArrays that contain
              > HashTableKeyVal ues<KEY_T, VALUE_T> ... obviously, it does not compile
              > because the notation is not correct (the compiler says that it needs a
              > real type, not a template).
              >
              > How can I implement this stuff? Do you think my logic is right?[/color]

              Read the FAQ 5.8


              Victor


              Comment

              Working...