Templates

A basic program but I can not get the template<> for the string to compile. Any help please.

#include <iostream>
#include <cstring> //pg 882 in textbook

using namespace std;

template< typename T >
T maximum( const T &first, const T &second )
{

if( first > second )
{
return first;
} else if( second > first )
{
return second;
}
}

template<>
string maximum<string>(string first, string second)
{
if(strcmp(first, second)==1)
{
return first;
}else if (strcmp(second, first)==1)
{
return second;
}
}
int main()
{
cout << maximum( 4, 6) << endl;
cout << maximum( 5.67, 5.34 ) << endl;
cout << maximum( 'c', 'e' ) << endl;
cout << maximum("dog", "dogs") << endl;
}
closed account (3TXyhbRD)
Hello,

Firstly, you can't use strcmp to compare strings, those functions are from the C standard where a string is described by a pointer to an array of characters (char*). Use the overloaded operator ==, < and > to compare two objects of type string.

Secondly, when you specialize your function, the arguments are of different types. Also when you call the last function, the C++ compiler can't determine if you're referring to string objects or char arrays.

Thirdly, what happens when first is equal to second?

PS: use code tags, they're awesome.
will do thank you.
If you explicitly specify the template argument when calling the function you don't really need to specialize the function.
cout << maximum<string>("dog", "dogs") << endl;

If you don't want to have to do that I think it's better that you make an overload of the maximum function that takes const char* as argument. Note that this is not a template specialization.
1
2
3
4
const char* maximum(const char* first, const char* second)
{
	...
}


The strcmp function does not necessary return exactly 1. All the standard says is that strcmp returns a value that is larger than 0 if the first string compares greater than the second, so instead of
if(strcmp(first, second)==1)
write
if(strcmp(first, second) > 0)
Last edited on
I have gotten it to work with the overload function, but need to compile a template specialization.

cout << maximum<string>("dog", "dogs") << endl;

i still cannot compile :
1
2
3
template<>
string maximum<string>(char *first, char *second)


I am alos interested in the overloading the ==, <, > to compare the object, but I am interested in what manner could I combine them to get that. I have only used overloading + and * to multiple and add two class objects together.

I hope I properly used the code tags!
Your template is declared as

1
2
template< typename T >
T maximum( const T &first, const T &second )

in which everywhere (in return and in the parameter declarations) is used T.

So your function speciialization is incorrect.
In fact you need not declare such specialization. If in your template you use operator < which is defined for class std::string, when you can call you template function the following way

maximum<std::string>( "dog", "dogs" );
closed account (3TXyhbRD)
Hey,

The code tags are fine now, much more pretty this way, yes?
You still get compilation errors because when you specialize your template, the formal parameters have different types. In your template definition you have:
1
2
template< typename T >
T maximum( const T &first, const T &second )

And when specializing you have:
1
2
template<>
string maximum<string>(char *first, char *second)

Your specialization doesn't have const char*& type parameters, they are just char* type parameters.
And you may want to write your specialization like this:
1
2
template<>
char* maximum<char*>(char*& first, char*& second)


String documentation:
http://www.cplusplus.com/reference/string/string/

Operator overload documentation:
http://www.cplusplus.com/doc/tutorial/classes2/
http://msdn.microsoft.com/en-us/library/5tk49fh2%28v=vs.80%29.aspx

If you overloaded operator + for objects, other operators work just the same, only that comparison operators (like > or ==) may return bool since it's a true/false context for these type of operators.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

class A
{
private:
    int _member;

public:
    A(int member): _member(member) { }

    bool operator < (const A& other)
    {
        return (this->_member < other._member);
    }
};

int main()
{
    A object1(2), object2(3);
    if (object1 < object2)
        cout<<"2 is smaller than 3"<<endl;
    else
        cout<<"3 is smaller than 2"<<endl;
    return 0;
}
Do you mean that we can overload the template variable like this .
1
2
3
4
5
6
7
8
9
10
11
template<T>
Tmaximum(T& first, T&second)
{
		if(strcmp(first, second)==1)
		{
				return first;
		}else if (strcmp(second, first)==1)
		{
				return second;
		}
}


please comment as i am curious about overloading the template function.. i have not tried it even once ..

but i suppose i have to try one good example as to understand the concept .
please feel free to give the example or link to the article so that the concept will be clear to me .
thanks in advance
Last edited on
closed account (3TXyhbRD)
The following code lines use an overload function, note the function is overloaded, it's not generated after your template! It exists even if you don't use char* type for maximum function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstring>

using namespace std;

template <typename Type>
Type maximum(Type a, Type b)
{
    if (a >= b)
        return a;
    else
        return b;
}

char* maximum(char* a, char* b)
{
    if (strcmp(a, b) >= 0)
        return a;
    else
        return b;
}

int main()
{
    char a[] = {"12"}, b[] = {"11"};
    cout<<maximum<int>(1, 2)<<endl;
    cout<<maximum(a, b)<<endl;
    return 0;
}

In the next code lines the template is specialized, the function is only generated (it only exists) if and only if you use the function with a char* type.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstring>

using namespace std;

template <typename Type>
Type maximum(Type a, Type b)
{
    if (a >= b)
        return a;
    else
        return b;
}

template <>
char* maximum<char*>(char* a, char* b)
{
    if (strcmp(a, b) >= 0)
        return a;
    else
        return b;
}

int main()
{
    char a[] = {"12"}, b[] = {"11"};
    cout<<maximum<int>(1, 2)<<endl;
    cout<<maximum<char*>(a, b)<<endl;
    return 0;
}
Topic archived. No new replies allowed.