[SOLVED]Return Template , It says T does not name a type

I'm doing assignment where i make two function template with two arguments that return the the Max and the Min. While im compiling the program, it says T does not name a type? any reason , or and way i can approach this problem.


# include <iostream>
using namespace std;

template <class T1>
T min(T &val1, T &val2)
{
T low;
if(val1<val2)
{
return val1;

}
else if (val2<val1)
{

return val2;
}
}

template <class T1, class T2>
T max (T1 &valu1, T2 &valu2)
{
T max;


if(valu1>valu2)
{
return valu1;
}
else if(valu2>valu1)
{
return valu2;
}
}

int main ()
{
int num1=5;
int num2=7;
double num=6.76;
char ch1='Z';

cout<<min(num1,num2)<<endl;
cout<<max(num1,num2)<<endl;
return 0;
}
Last edited on
Please use code tags when posting code.

Be careful naming your classes min and max, there are already standard functions with those names. Speaking of the standard functions why aren't you using those standard functions instead?

As to your compiler errors:
1
2
template <class T1>
T min(T &val1, T &val2)

Where is that magical type "T" defined, should it be "T1" instead?

the assignment that my teacher instruct:
Write templates for the two functions minimum and maximum. The minimum function should accept two arguments and return the value of the argument that is the lesser of the two. The maximum function should accept two arguments and return the value of the argument that is the greater of the two. Design a simple driver program that demonstrates the templates with various data types.

# include <iostream>
using namespace std;

template <class T1,class T2>
T mi(T1 &val1, T2 &val2)
{
T low;
if(val1<val2)
{
return val1;

}
else if (val2<val1)
{

return val2;
}
}

template <class T1, class T2>
T ma (T1 &valu1, T2 &valu2)
{
T max;


if(valu1>valu2)
{
return valu1;
}
else if(valu2>valu1)
{
return valu2;
}
}

int main ()
{
int num1=5;
int num2=7;
double num=6.76;
char ch1='Z';

cout<<mi(num1,num2)<<endl;
cout<<ma(num1,num2)<<endl;
return 0;
}



Still says the same thing when i change the function.

How to use code tags? sorry newbie here.
Last edited on

1
2
template <class T1,class T2>
T mi(T1 &val1, T2 &val2)

Again where is that magical "T" coming from? Since your parameters are different what type are you going to return. And how can you compare two incompatible types, ie: how do you compare a string to an int?

The minimum function should accept two arguments and return the value of the argument that is the lesser of the two.

This, IMO, implies that the two parameters should be of the same type and the return type should also be the same.

How to use code tags?

Use the <> icon to the right of the edit window.
For more instructions on how to use code tags - and other tags - to format your posts, look here:

http://www.cplusplus.com/articles/z13hAqkS/
OP: you may be getting your types and arguments confused a bit ...
note your problem says ...
The minimum function should accept two arguments and return the value of the argument that is the lesser of the two.
i.e two arguments, not 2 types and of course both arguments have to be of the same type otherwise we'd be comparing the proverbial apples with oranges etc.
Now look at the function template declaration:
template <class T1, class T2> // use typename instead of class which also means something else in C++;
here T1 and T2 are types, not parameters and in the case of your min and max functions, as discussed there is only one type of parameters passed to the function, so we could simplify to ...
template <typename T>
Now how many parameters do we need ... well, two, so we have further

1
2
3
4
5
6
 template <typename T>
const T& minimum(const T& val1, const T& val2)
//good u use ref, also use const as function shouldn't change passed parameters
{
	return val1 < val2 ? val1 : val2; // you can simplify your code using the ternary operator
}

now let's use our template function:
1
2
3
4
5
6
7
8
9
10
#include<iostream>

int main()
{
	int x = 6, y = 12; 
	std::cout<<"Minimum of integers 6 and 12 is:"<<minium(x, y)<<"\n";
	
	auto x = 17.5, y = 22.0;//C++11 automatic type deduction feature; 
	std::cout<<"Minimum of auto declared variables 17.5 and 22 is:"<<minimum(17.5, 22)<<"\n";
}
Last edited on
1) The T which is a Template was suppose to return any data type.
2) I wanted to return any type of data which the T can do.
which it work for this program in the book
1
2
3
4
5
6
7
Here is a function template for the square function:
template <class T>
T square(T number)
{
return number * number;
but wanted to use if statements and two arguments, but your right,  doesn't make sense for you to compare different types of data 
} 

3)I can compare two incompatible data type by converting each types into bytes.

Thank you for posting that question, made me think carefully , I can't measure two different data types unless i turn them into compatible types by using size of.

Just thought that the template would make it already compatible without converting anything.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# include <iostream>
using namespace std;

template <class T1,class T2>
int min(T1 &num1, T2 &num2 )
{
	if (sizeof(num1)<sizeof(num2))
		return sizeof(num1);
	else if (sizeof(num2)<sizeof(num1))
		return sizeof(num2);

}

template <class T1, class T2>
int max (T1 &numm1, T2 &numm2)
{
	if (sizeof(numm1)>sizeof(numm2))
		return sizeof(numm1);
	else if (sizeof(numm2)>sizeof(numm1))
		return sizeof(numm2);

}

int main()
{
	int ival=5;
	double dval=5.45;
	char cval= 'f';
	string sval= "hello";	
	
		
	
	
	
	
	cout<<"1     "<<"2    "<<"Size Of 1    "<< "Size of 2   "<<"MIN      "<<"MAX"
	<<endl<<"------------------------------------------------"<<endl;
	cout<<ival<<"   "<<dval<<"       "<<sizeof(ival)<<"            "<<sizeof(dval)<<"       "<<min(ival,dval)<<"      "<<max(ival,dval)<<endl;
	cout<<cval<<"   "<<sval<<"      "<<sizeof(cval)<<"            "<<sizeof(sval)<<"       "<<min(cval,sval)<<"      "<<max(cval,sval)<<endl;
	
	return 0;
}


/*1     2    Size Of 1    Size of 2   MIN      MAX
------------------------------------------------
5   5.45       4            8       4      8
f   hello      1            8       1      8

--------------------------------
Process exited after 0.009415 seconds with return value 0
Press any key to continue . . .*/




I can compare two incompatible data type by converting each types into bytes.


the return value of sizeof() of parameters of different types would still be of the same type, so no matter how you slice and dice it you can't get from the same type at the point of actual comparison
Last edited on
Requires C++14:
1
2
template < typename A, typename B >
auto min( const A& a, const B& b ) { return a<b ? a : b ; }


C++11:
1
2
template < typename A, typename B >
auto min( const A& a, const B& b ) -> decltype( a<b ? a : b ) { return a<b ? a : b ; }


Also (#include <type_traits>)
1
2
template < typename A, typename B >
typename std::common_type<A,B>::type min( const A& a, const B& b ) { return a<b ? a : b ; }
Spoke to my professor said follow this format

1
2
3
4
5
6
7
8
9
You have to use this format:
template <class T>
T minimum(T number1, T number2)

You should use value because you are not changing the value of the variables.

If a variable is 7.7, you should use double not int.

You should find the largest or smallest value for the numbers.  The functions min and max should be overloaded so that they can run for both int and double.



So i did what he said, and it finally work, Thx for the help.


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# include <iostream>
using namespace std;

template <class T>
T min(T &num1, T &num2 )
{
	if ((num1)<(num2))
		return (num1);
	else if ((num2)<(num1))
		return (num2);

}

template <class T>
T max (T &numm1, T &numm2)
{
	if ((numm1)>(numm2))
		return (numm1);
	else if ((numm2)>(numm1))
		return (numm2);

}

int main()
{
	int ival=5;
	int ival2=6;
	double dval1=6.8;
	double dval2=7.9;
	char cval1='a';
	char cval2='z';	
	
		
	
	
	
	
	cout<<"1     "<<"2    "<<"MIN      "<<"MAX"
	<<endl<<"------------------------------------------------"<<endl;
	cout<<ival<<"     "<<ival2<<"      "<<min(ival,ival2)<<"      "<<max(ival,ival2)<<endl;
	cout<<dval1<<"   "<<dval2<<"    "<<min(dval1,dval2)<<"    "<<max(dval1,dval2)<<endl;
	cout<<cval1<<"     "<<cval2<<"      "<<min(cval1,cval2)<<"      "<<max(cval1,cval2)<<endl;
	
	return 0;
}


/*1     2    MIN      MAX
------------------------------------------------
5     6      5      6
6.8   7.9    6.8      7.9
a     z      a      z

--------------------------------
Process exited after 0.008845 seconds with return value 0
Press any key to continue . . .*/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

template <class T>
T min(T &num1, T &num2 )
{
	if ((num1)<(num2))
		return (num1);
	else if ((num2)<(num1))
		return (num2);

} // *** warning: control may reach end of non-void function (clang++)
  // *** warning: control reaches end of non-void function (g++)
  // *** warning: 'min<int>': not all control paths return a value (microsoft)

int main()
{
    int a = 7, b = 7 ;
    int c = min(a,b) ; // *** undefined behaviour
    return c - 7 ;
}

http://coliru.stacked-crooked.com/a/d20686f18e201689
http://rextester.com/JMCIV20652

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

template <class T>
T min(T &num1, T &num2 )
{
	if ((num1)<(num2))
		return (num1);
	else if ((num2)<(num1))
		return (num2);

} // *** warning: control reaches end of non-void function

int main()
{
    int a = 7, b = 7 ;
    int c = min( +a, b ) ; // *** error: no matching function for call to 'min' (clang++)
                           // ***  note: candidate function [with T = int] not viable: expects an l-value for 1st argument (clang++)
    
                           // *** error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' (g++)
                           // ***  note: initializing argument 1 of 'T min(T&, T&) [with T = int]' (g++)
    
                           // *** error: 'T min<int>(T &,T &)': cannot convert argument 1 from 'int' to 'int &' [with T = int] (microsoft)
}

http://coliru.stacked-crooked.com/a/a4f98e97b1fc7d3b
http://rextester.com/JCCZW92172
Topic archived. No new replies allowed.