Thread: Problem with templates

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33

    Problem with templates

    I'm trying an exercise in a book I read, which basically says "Write a template function that takes as an argument an array of five items of type T, and returns the largest item. Test it in a program with an array of 5 int values and 5 double values."
    I've tried everything, but did not get it to work. There is my code:
    Code:
    #include <iostream>
    using namespace std;
    template <typename T>
    T max5(T,int);
    int main()
    {
    	int numbers[5] = {0};
    	for (int i = 0; i < 5; i++)
    	{
    		cout << "Enter the number for element num " << (i+1) << "\n";
    		cin >> numbers[i];
    	}
    	cout << "The largest number in the array is: ";
    	int max = max5(numbers, 5);
    	cout << max << "\n";
    	return 0;
    }
    template <typename T>
    T max5(T arr[],int n)
    {
    	T max = 0;
    	for (int i=0; i<n; i++)
    		if arr[i]>max
    			max = arr[i];
    	return max;
    }
    I get an error, saying:
    .\Source1.cpp(14) : error C2440: 'initializing' : cannot convert from 'int *' to 'int'
    There is no context in which this conversion is possible

    Line 14 is:
    Code:
    int max = max5(numbers, 5);
    BTW, I defined the maximum number as int only becuase I don't know how to tell the compiler to check wheter it is an int, double, just as the exercise says.
    Last edited by ThWolf; 09-05-2006 at 06:07 AM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The function will fail, or rather won't work, if all the numbers are negative, so I'd recommend changing

    >> T max = 0;

    to

    >> T max = arr[0];

    or whatever it was called, and then continuing with the loop from i=1

    Also,

    >> if arr[i]>max

    shouldn't that be

    >> if (arr[i]>max )

  3. #3
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Thanks, I always forget that if statements have to be in brackets. Anyway, its not working.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your prototype is different than the function definition. Make sure they match.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    This isn't directly related to your problem, but there's a trick you can use with template parameter deduction to simplify the use of your function a bit:
    Code:
    template <typename T>
    T max5(T,int);
    can change to
    Code:
    // change the function prototype
    template <typename T, int n>
    T max5(T (&)[n] );


    and the function definition changes accordingly
    Code:
    template <typename T>
    T max5(T arr[],int n)
    {
    	T max = 0;
    	for (int i=0; i<n; i++)
    		if arr[i]>max
    			max = arr[i];
    	return max;
    }
    to
    Code:
    template <typename T, int n>
    T max5(T (&arr)[n] )
    {
    	T max = 0;
    	for (int i=0; i<n; i++)
    		if (arr[i]>max)
    			max = arr[i];
    	return max;
    }


    Then change your call from
    Code:
     int max = max5(numbers, 5);
    to
    Code:
     int max = max5(numbers);
    in short, you can "deduce" the size of a statically sized array without needing to do any work. The prototype might look slightly more cryptic, but should make actual use of the function a bit easier
    Last edited by Bench82; 09-05-2006 at 05:28 PM.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Thanks Bench82, that did the job, though I can't really understand what you've done. Am I passing a reference, instead of the array? I hope you can explain it to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM