CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Help with Specialization error

    you simply forgot the '*':

    template <> char* maxn<char*>(char* str[], int ct);

  3. #3
    Join Date
    May 2010
    Posts
    54

    Re: Help with Specialization error

    Quote Originally Posted by superbonzo View Post
    you simply forgot the '*':
    I'm just stupid, but shouldn't it be a best match even with out the * ?

  4. #4
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Help with Specialization error

    Quote Originally Posted by iExtreme View Post
    I'm just stupid, but shouldn't it be a best match even with out the * ?
    It's not a "best match" because it's not even a legal definition. The signature of a function template specialization must match the signature of the to be specialized function template.

    So you can write

    Code:
    template <> char* maxn<char>(char* str[], int ct); // illegal, it's a potential specialization of a function template with signature template <class  T> T* maxn(T* str[],int);
    
    template <> char* maxn<char*>(char* str[], int ct); // ok, it's a specialization of a function template with signature template <class  T> T maxn(T str[],int);
    
    template <> char* maxn(char* str[], int ct); // ok, as above; but this time the type is deduced
    
    char* maxn(char* str[], int ct); // ok, a function overload

  5. #5
    Join Date
    May 2010
    Posts
    54

    Re: Help with Specialization error

    Quote Originally Posted by superbonzo View Post
    It's not a "best match" because it's not even a legal definition. The signature of a function template specialization must match the signature of the to be specialized function template.

    So you can write

    Code:
    template <> char* maxn<char>(char* str[], int ct); // illegal, it's a potential specialization of a function template with signature template <class  T> T* maxn(T* str[],int);
    
    template <> char* maxn<char*>(char* str[], int ct); // ok, it's a specialization of a function template with signature template <class  T> T maxn(T str[],int);
    
    template <> char* maxn(char* str[], int ct); // ok, as above; but this time the type is deduced
    
    char* maxn(char* str[], int ct); // ok, a function overload
    Oh, thanks for the clarification, but I think using the first form is the best? because type deduction isn't standard C++ as far as I know, thank you very much for you help.

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