template woes

This is a discussion on template woes within the C++ Programming forums, part of the General Programming Boards category; Need some help with this template example on the prototype to the function I used the generic type from the ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    template woes

    Need some help with this template example
    on the prototype to the function I used the generic type from the template declaration t. Is this valid. C++ Primer Plus only used void as the return type, but as you see i want the return type to be either double or integer depending on what is sent.

    Code:
    // template program prblm 5 pg 370
    #include <iostream>
    using namespace std;
    
    // template , prototypes
    template <typename t>
    t max5(t &ar);
    
    int main()
    {
    	int numi[5]={10,4,5,9,8};
    	double numd[5]={10.1,30.2,15.34,70.1,80};
    
    	for (int i=0; i<5;i++)
    	{
    		cout << "Integer array element #"<< i<<" ="<< numi[i] << ".\t";
    		cout << "Double  array element #"<< i<<" ="<< numd[i] << ".\n";
    	}
    	cout <<"Greatst element of integer array="<<max5(numi)<<endl;// error here
    	cout <<"Greatst element of double array="<<max5(numd)<<endl;
    	return 0;
    }
    
    template <typename t>
    t max5(t &ar)
    {
    	t max;
    	for (int=0;i<4;i++)
    	{
    		if (ar[i]>ar[i+1])
    			max=ar[i];
    	}
    	return max;
    }
    here are the errors
    C:\Borland\BCC55\src>bcc32 temp.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    temp.cpp:
    Error E2094 temp.cpp 19: 'operator<<' not implemented in type 'ostream' for arguments of type 'int[5]' in function main()
    Error E2094 temp.cpp 20: 'operator<<' not implemented in type 'ostream' for arguments of type 'double[5]' in function main()
    *** 2 errors in Compile ***

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    t in your case is an array, so the errors make sense. Try something like -

    template <typename t>
    t max5(t ar[5]);

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    52

    replacing &ar with ar[5]

    I get a whole new set of errors replacing &t with ar[5]

    C:\Borland\BCC55\src>bcc32 temp.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    temp.cpp:
    Error E2094 temp.cpp 19: 'operator<<' not implemented in type 'ostream' for argu
    ments of type 'int[5]' in function main()
    Error E2094 temp.cpp 20: 'operator<<' not implemented in type 'ostream' for argu
    ments of type 'double[5]' in function main()
    *** 2 errors in Compile ***

    I attmpted to replace it in the prototype alone then in both the prototype and function definiton. t resolves to an array of type (int or double) but i want the return type to be either int or double depending on the array. How do I implemnt this in my template?

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >I get a whole new set of errors replacing &t with ar[5]

    Well you would do. Was this a typo?

    This works (but not tested on Borland) -

    Code:
    #include <iostream>
    using namespace std;
    
    // template , prototypes
    template <typename t>
    t max5(t ar[5]);
    
    int main()
    {
    	int numi[5]={10,4,5,9,8};
    	double numd[5]={10.1,30.2,15.34,70.1,80};
    
    	for (int i=0; i<5;i++)
    	{
    		cout << "Integer array element #"<< i<<" ="<< numi[i] << ".\t";
    		cout << "Double  array element #"<< i<<" ="<< numd[i] << ".\n";
    	}
    	cout <<"Greatst element of integer array="<<max5(numi)<<endl;// error here
    	cout <<"Greatst element of double array="<<max5(numd)<<endl;
    	return 0;
    }
    
    template <typename t>
    t max5(t ar[5])
    {
    	
    	t max_=0;
    	for (int i =0;i<4;i++)
    	{
    		if (ar[i]>max_)
    			max_=ar[i];
    	}
    	return max_;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    here are the errors i get using the code

    Code:
    #include <iostream>
    using namespace std;
    
    // template , prototypes
    template <typename t>
    t max5(t ar[5]);
    
    int main()
    {
    	int numi[5]={10,4,5,9,8};
    	double numd[5]={10.1,30.2,15.34,70.1,80};
    
    	for (int i=0; i<5;i++)
    	{
    		cout << "Integer array element #"<< i<<" ="<< numi[i] << ".\t";
    		cout << "Double  array element #"<< i<<" ="<< numd[i] << ".\n";
    	}
    	cout <<"Greatst element of integer array="<<max5(numi)<<endl;// error here
    	cout <<"Greatst element of double array="<<max5(numd)<<endl;
    	return 0;
    }
    
    template <typename t>
    t max5(t ar[5])
    {
    	
    	t max_=0;
    	for (int i =0;i<4;i++)
    	{
    		if (ar[i]>max_)
    			max_=ar[i];
    	}
    	return max_;
    }
    C:\Borland\BCC55\src>bcc32 temp.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    temp.cpp:
    Error E2040 temp.cpp 28: Declaration terminated incorrectly in function max5<int
    >(int *)
    Error E2451 temp.cpp 28: Undefined symbol 'i' in function max5<int>(int *)
    Warning W8057 temp.cpp 34: Parameter 'ar' is never used in function max5<int>(in
    t *)
    Error E2040 temp.cpp 28: Declaration terminated incorrectly in function max5<dou
    ble>(double *)
    Error E2451 temp.cpp 28: Undefined symbol 'i' in function max5<double>(double *)

    Warning W8057 temp.cpp 34: Parameter 'ar' is never used in function max5<double>
    (double *)
    *** 4 errors in Compile ***

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    t max5(t ar[5])

    Here you are only passing one value to the function. Instead, try t ar[].

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    52
    I belive the problem lies with the function type not with the passing by refrence t &a. t should resolve to int or double array, but the return type may not resolve to an int or double value. Logicly how could it resolve to both, for instance an int for the return type and int array for the parameter. This is my first attempt using a template. If there is a way to create a generic return type let me know.
    Here is code with function return type set to double and the logic error (to find the max element of an array) fixed. It works though I wanted a generic return type to be set by the template.
    Code:
    // template program prblm 5 pg 370
    #include <iostream>
    using namespace std;
    
    // template , prototypes
    template <typename t>
    double max5(t &ar);
    
    int main()
    {
    	int numi[5]={10,4,5,9,8};
    	double numd[5]={10.1,30.2,15.34,70.1,80};
    
    	for (int i=0; i<5;i++)
    	{
    			cout << "Integer array element #"<< i<<" ="<< numi[i] << ".\t";
    			cout << "Double  array element #"<< i<<" ="<< numd[i] << ".\n";
    	}
    	cout <<"Greatst element of integer array="<<max5(numi)<<endl;
    	cout <<"Greatst element of double array="<<max5(numd)<<endl;
    	return 0;
    }
    
    template <typename t>
    double max5(t &ar)
    {
    	double max=0;
    	for (int i=0;i<5;i++)
    	{
    		if (max<ar[i])
    		{max=ar[i];}
    
    	}
    	return max;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21