Unresolved externals

I'm studying templates but can't find how to write a prototype for it. 1st code is function and 2nd is prototype. Help me please. Sorry for my bad endlish

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename A, typename B>
 char CompareNum(A a, B b)
{
	if (a == b)
	{
		return '=';
	}
	else if (a > b)
	{
		return  '>';
	}
	else
	{
		return '<';
	}
}


1
2
template<typename A, typename B>
 char CompareNum(A a, B b);
Your code is correct, but note that you have to define the template in each translation unit that uses it. Templates are often defined in the header files because of this.

@Peter's right, your code should look like this(also I'd prefer to use class in this case, because they will be the same type.):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<class t>
 char CompareNum(t a, t b)
{
	if (a == b)
	{
		return '=';
	}
	else if (a > b)
	{
		return  '>';
	}
	else
	{
		return '<';
	}
}
Last edited on
your code should look like this(also I'd prefer to use class in this case, because they will be the same type.):

Your assumption that the arguments will be of the same type is unwarranted. The use of class or typename here is completely subjective and a matter of style.
Ok, I wasn't sure about that, because I am also a beginner sort of.
Topic archived. No new replies allowed.