Unresolved externals

Jan 30, 2016 at 9:44pm
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);
Jan 30, 2016 at 10:00pm
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.
Jan 30, 2016 at 10:25pm

@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 Jan 30, 2016 at 10:26pm
Jan 30, 2016 at 11:35pm
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.
Jan 31, 2016 at 1:14pm
Ok, I wasn't sure about that, because I am also a beginner sort of.
Topic archived. No new replies allowed.