CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    May 2010
    Posts
    54

    Help with Specialization error

    Hey guys, I'm doing a simple program that uses a template function to take an array of any type and returns the largest item in the array, but I also want to use a specialization that takes an array of pointer to char argument and returns the address of the largest string, I'm required to use specialization to make it work.

    Here's my code:

    Code:
    // ex86.cpp -- 6th exercise-8th chapter
    
    #include <iostream>
    #include <cstring>
    
    template <typename T>
    T maxn(T arr[], int ct);
    
    template <> char* maxn<char>(char* str[], int ct);
    
    int main()
    {
    	using namespace std;
    	int iarr[6];
    	cout << "Enter 6 integers:\n";
    	for (int i = 0; i < 6; i++)
    	{
    		cout << "Integer #" << i + 1 << ": ";
    		cin >> iarr[i];
    	}
    	double darr[4];
    	cout << "Enter 4 doubles:\n";
    	for (int i = 0; i < 4; i++)
    	{
    		cout << "Double #" << i + 1 << ": ";
    		cin >> darr[i];
    	}
    	int max_int;
    	double max_double;
    	max_int = maxn(iarr, 6);
    	max_double = maxn(darr, 4);
    	cout << "Max integer value = " << max_int << endl;
    	cout << "Max double value = " << max_double << endl;
    
    	char *str[5];
    	for (int i = 0; i < 5; i++)
    		str[i] = new char[80];
    	cout << "Enter 5 strings [max 80 chars]:\n";
    	for (int i = 0; i < 5; i++)
    	{
    		cout << "String #" << i + 1 << ": ";
    		cin.getline(str[i], 80);
    	}
    	char *cp = maxn(str, 5);
    	cout << "The longest string is: " << cp << endl;
    	return 0;
    }
    
    template <typename T>
    T maxn(T arr[], int ct)
    {
    	T max = arr[0];
    	for (int i = 1; i < ct; i++)
    		if (arr[i] > max)
    			max = arr[i];
    	return max;
    }
    
    template <> char* maxn<char>(char* str[], int ct)
    {
    	char* max = str[0];
    	for (int i = 1; i < ct; i++)
    		if (strlen(str[1]) > strlen(max))
    			max = str[i];
    	return max;
    }
    Now whenever I compile I get that error in VS2010:

    error C2912: explicit specialization; 'char *maxn<char>(char *[],int)' is not a specialization of a function template

    I made another instance of the code using non-template function to overload when using char* and the program worked like a charm, here's the code:

    Code:
    // ex86.cpp -- 6th exercise-8th chapter
    
    #include <iostream>
    #include <cstring>
    
    template <typename T>
    T maxn(T arr[], int ct);
    
    char* maxn(char* str[], int ct);
    
    int main()
    {
    	using namespace std;
    	int iarr[6];
    	cout << "Enter 6 integers:\n";
    	for (int i = 0; i < 6; i++)
    	{
    		cout << "Integer #" << i + 1 << ": ";
    		cin >> iarr[i];
    	}
    	double darr[4];
    	cout << "Enter 4 doubles:\n";
    	for (int i = 0; i < 4; i++)
    	{
    		cout << "Double #" << i + 1 << ": ";
    		cin >> darr[i];
    	}
    	int max_int;
    	double max_double;
    	max_int = maxn(iarr, 6);
    	max_double = maxn(darr, 4);
    	cout << "Max integer value = " << max_int << endl;
    	cout << "Max double value = " << max_double << endl;
    	cin.get();
    
    	char *str[5];
    	for (int i = 0; i < 5; i++)
    		str[i] = new char[80];
    	cout << "Enter 5 strings [max 80 chars]:\n";
    	for (int i = 0; i < 5; i++)
    	{
    		cout << "String #" << i + 1 << ": ";
    		cin.getline(str[i], 80);
    	}
    	char *cp = maxn(str, 5);
    	cout << "The longest string is: " << cp << endl;
    	return 0;
    }
    
    template <typename T>
    T maxn(T arr[], int ct)
    {
    	T max = arr[0];
    	for (int i = 1; i < ct; i++)
    		if (arr[i] > max)
    			max = arr[i];
    	return max;
    }
    
    char* maxn(char* str[], int ct)
    {
    	char* max = str[0];
    	for (int i = 1; i < ct; i++)
    		if (strlen(str[1]) > strlen(max))
    			max = str[i];
    	return max;
    }
    However, in the original quiz I'm required to use specialization, can anyone please point to what I did do wrong in the first instance of code?

    Thanks all for your help.
    Last edited by iExtreme; July 3rd, 2010 at 02:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured