Template issue

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

    Template issue

    I create an templated Array class. My constructor declaration is as
    follows,
    template<class X> Array<X>::Array (int s); // Constructor

    I also have templated sorting function,
    template<class X> void Quicksort(X values[], int first, int last);

    I am getting the following compilation error,

    error C2784: 'void Quicksort(X [],int,int)' : could not deduce
    template argument for 'T1 []' from 'Array<X>'
    with
    [
    X=std::string
    ]

    My dirver code,

    // Read in an array of strings
    cout << "How many words? ";
    cin >> num;
    Array<string> b(num);
    cout << "Enter the " << num << " words below:\n";
    for (int i=0; i<num ; i++) cin >> b[i];

    // Copy the original array and sort it using Quicksort
    Array<string> bq(b);
    Quicksort(bq, 0, num-1);
    cout << "\nElements sorted using quicksort:\n";
    for (int i=0; i<num ; i++) cout << bq[i]<< " ";

    Don't know what's wrong. Any help is appreciated,
    TIA,
    Ves


  • John Harrison

    #2
    Re: Template issue


    "Vespasian" <julie.spicer@v erizon.net> wrote in message
    news:obeva0l4rr ptmsnqqburarf77 jqeiotgih@4ax.c om...[color=blue]
    > I create an templated Array class. My constructor declaration is as
    > follows,
    > template<class X> Array<X>::Array (int s); // Constructor
    >
    > I also have templated sorting function,
    > template<class X> void Quicksort(X values[], int first, int last);
    >
    > I am getting the following compilation error,
    >
    > error C2784: 'void Quicksort(X [],int,int)' : could not deduce
    > template argument for 'T1 []' from 'Array<X>'
    > with
    > [
    > X=std::string
    > ][/color]

    Simple enough, your Quicksort routine expects X[], you are giving it
    Array<X>. They aren't the same.

    john



    Comment

    • Vespasian

      #3
      Re: Template issue

      On Sat, 22 May 2004 22:49:12 +0100, "John Harrison"
      <john_andronicu s@hotmail.com> wrote:
      [color=blue]
      >
      >"Vespasian" <julie.spicer@v erizon.net> wrote in message
      >news:obeva0l4r rptmsnqqburarf7 7jqeiotgih@4ax. com...[color=green]
      >> I create an templated Array class. My constructor declaration is as
      >> follows,
      >> template<class X> Array<X>::Array (int s); // Constructor
      >>
      >> I also have templated sorting function,
      >> template<class X> void Quicksort(X values[], int first, int last);
      >>
      >> I am getting the following compilation error,
      >>
      >> error C2784: 'void Quicksort(X [],int,int)' : could not deduce
      >> template argument for 'T1 []' from 'Array<X>'
      >> with
      >> [
      >> X=std::string
      >> ][/color]
      >
      >Simple enough, your Quicksort routine expects X[], you are giving it
      >Array<X>. They aren't the same.
      >
      >john
      >[/color]

      I changes my declartation to and still get similiar error,

      template<class X> void Quicksort(Array <X> values[], int first, int
      last);

      c:\Files\docume nts\dataStuctur es_HW\hw7\gener ic.cpp(55): error C2784:
      'void Quicksort(Array <X> [],int,int)' : could not deduce template
      argument for 'Array<X> []' from 'Array<X>'
      with
      [
      X=std::string
      ]


      Comment

      • Jorge Rivera

        #4
        Re: Template issue

        Vespasian wrote:[color=blue]
        > On Sat, 22 May 2004 22:49:12 +0100, "John Harrison"
        > <john_andronicu s@hotmail.com> wrote:
        >
        >[color=green]
        >>"Vespasian" <julie.spicer@v erizon.net> wrote in message
        >>news:obeva0l4 rrptmsnqqburarf 77jqeiotgih@4ax .com...
        >>[color=darkred]
        >>>I create an templated Array class. My constructor declaration is as
        >>>follows,
        >>>template<cla ss X> Array<X>::Array (int s); // Constructor
        >>>
        >>>I also have templated sorting function,
        >>>template<cla ss X> void Quicksort(X values[], int first, int last);
        >>>
        >>>I am getting the following compilation error,
        >>>
        >>>error C2784: 'void Quicksort(X [],int,int)' : could not deduce
        >>>template argument for 'T1 []' from 'Array<X>'
        >>> with
        >>> [
        >>> X=std::string
        >>> ][/color]
        >>
        >>Simple enough, your Quicksort routine expects X[], you are giving it
        >>Array<X>. They aren't the same.
        >>
        >>john
        >>[/color]
        >
        >
        > I changes my declartation to and still get similiar error,
        >
        > template<class X> void Quicksort(Array <X> values[], int first, int
        > last);
        >[/color]

        Should be

        template<class X> void Quicksort(X values, int first, int last);

        with X=Array<string>

        JLR

        Comment

        • Sergiy Kanilo

          #5
          Re: Template issue


          "Vespasian" <julie.spicer@v erizon.net> wrote in message
          news:r5kva09n1b mji9j0lfe4pp679 uniicolul@4ax.c om...
          [color=blue][color=green][color=darkred]
          > >> I am getting the following compilation error,
          > >>
          > >> error C2784: 'void Quicksort(X [],int,int)' : could not deduce
          > >> template argument for 'T1 []' from 'Array<X>'[/color][/color][/color]

          [...]
          [color=blue]
          > I changes my declartation to and still get similiar error,
          >
          > template<class X> void Quicksort(Array <X> values[], int first, int
          > last);[/color]

          You can try

          template<class X> void Quicksort(Array <X>& values, int first, int last)
          {
          ....
          }

          or even

          template<class X> void Quicksort(X& values, int first, int last)
          {
          ....
          }

          I believe that both variants will work (of course if your implementation
          of Quicksort is correct)

          Cheers,
          Serge


          Comment

          • Vespasian

            #6
            Re: Template issue

            On Sat, 22 May 2004 18:35:34 -0400, "Sergiy Kanilo"
            <skanilo@artann labs.com> wrote:
            [color=blue]
            >
            >"Vespasian" <julie.spicer@v erizon.net> wrote in message
            >news:r5kva09n1 bmji9j0lfe4pp67 9uniicolul@4ax. com...
            >[color=green][color=darkred]
            >> >> I am getting the following compilation error,
            >> >>
            >> >> error C2784: 'void Quicksort(X [],int,int)' : could not deduce
            >> >> template argument for 'T1 []' from 'Array<X>'[/color][/color]
            >
            >[...]
            >[color=green]
            >> I changes my declartation to and still get similiar error,
            >>
            >> template<class X> void Quicksort(Array <X> values[], int first, int
            >> last);[/color]
            >
            >You can try
            >
            >template<cla ss X> void Quicksort(Array <X>& values, int first, int last)
            >{
            >...
            >}
            >
            >or even
            >
            >template<cla ss X> void Quicksort(X& values, int first, int last)
            >{
            >...
            >}
            >
            >I believe that both variants will work (of course if your implementation
            >of Quicksort is correct)
            >
            >Cheers,
            >Serge
            >[/color]


            Thanks, Serge -- that worked

            Comment

            Working...