Template issue

Discussion in 'C++' started by Vespasian, May 22, 2004.

  1. Vespasian

    Vespasian Guest

    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;

    // 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<< " ";

    Don't know what's wrong. Any help is appreciated,
    TIA,
    Ves
    Vespasian, May 22, 2004
    #1
    1. Advertising

  2. "Vespasian" <> wrote in message
    news:eek:...
    > 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
    > ]


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

    john
    John Harrison, May 22, 2004
    #2
    1. Advertising

  3. Vespasian

    Vespasian Guest

    On Sat, 22 May 2004 22:49:12 +0100, "John Harrison"
    <> wrote:

    >
    >"Vespasian" <> wrote in message
    >news:eek:...
    >> 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
    >> ]

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


    I changes my declartation to and still get similiar error,

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

    c:\Files\documents\dataStuctures_HW\hw7\generic.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
    ]
    Vespasian, May 22, 2004
    #3
  4. Vespasian

    Jorge Rivera Guest

    Vespasian wrote:
    > On Sat, 22 May 2004 22:49:12 +0100, "John Harrison"
    > <> wrote:
    >
    >
    >>"Vespasian" <> wrote in message
    >>news:eek:...
    >>
    >>>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
    >>> ]

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

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


    Should be

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

    with X=Array<string>

    JLR
    Jorge Rivera, May 22, 2004
    #4
  5. "Vespasian" <> wrote in message
    news:...

    > >> I am getting the following compilation error,
    > >>
    > >> error C2784: 'void Quicksort(X [],int,int)' : could not deduce
    > >> template argument for 'T1 []' from 'Array<X>'


    [...]

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


    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
    Sergiy Kanilo, May 22, 2004
    #5
  6. Vespasian

    Vespasian Guest

    On Sat, 22 May 2004 18:35:34 -0400, "Sergiy Kanilo"
    <> wrote:

    >
    >"Vespasian" <> wrote in message
    >news:...
    >
    >> >> I am getting the following compilation error,
    >> >>
    >> >> error C2784: 'void Quicksort(X [],int,int)' : could not deduce
    >> >> template argument for 'T1 []' from 'Array<X>'

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

    >
    >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
    >



    Thanks, Serge -- that worked
    Vespasian, May 23, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    468
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    534
    tom_usenet
    Jul 24, 2003
  3. IR
    Replies:
    3
    Views:
    420
  4. Replies:
    1
    Views:
    2,107
    Gianni Mariani
    Jun 8, 2007
  5. Replies:
    2
    Views:
    769
Loading...

Share This Page