Template issue

V

Vespasian

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
 
J

John Harrison

Vespasian said:
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
 
V

Vespasian

Vespasian said:
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
]
 
J

Jorge Rivera

Vespasian said:
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
 
S

Sergiy Kanilo

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
 
V

Vespasian

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top