|
-
April 13th, 2012, 08:49 PM
#1
function template
Write a function template contains that accepts an array of the parameterized type, the number of elements in the array (an integer), and a value of the parameterized type, and returns true if the value is in the array and false otherwise.
template<class T>
bool contains(T a[], int k, T p)
{
bool stat = false;
for(int i=0; i < k; i++)
{
if(a[i] == p)
{
stat = true;
}
}
return stat;
}
saying compiler error...help please
-
April 13th, 2012, 09:43 PM
#2
Re: function template
i think in line
Code:
bool contains(T a[], int k, T p)
you can insert Or then malloc memory for *a
-
April 14th, 2012, 02:58 AM
#3
Re: function template
 Originally Posted by mejiac3
saying compiler error...help please
I suggest that you post the smallest and simplest program that demonstrates this error, and also post what is the error. From what I see, your function template could compile, so perhaps the error lies elsewhere.
 Originally Posted by htuan_2005
Changing the declaration of parameter a to that won't make a difference, and in fact is wrong because it hints that the array is expected to have 10 elements when there is no such restriction.
 Originally Posted by htuan_2005
then malloc memory for *a
That does not make sense: the function is supposed to search the array to check if it contains a value. Obviously, the array must already exist, otherwise the instructions would have mentioned populating the array in the function. Furthermore, malloc may be inappropriate to create objects of type T.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|