Template help

Hi im learning about templates and i need to make an isequal function that can accept ints, doubles and chars and find out if they're equal but i don't understand how i can pass a variable into function unless i make a variable of each type and then copy the input into each one and pass them all through the function which seems excessive and to defeat the purpose.

i've tried using a union for my input variables but im having trouble calling the template now.

heres my code for my 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
template <typename T>
void IsEqual (const T* input1, const T * input2)
{
	if(input1 == input2)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
int main()
{
	int result;
	union
	{
		double value1;
		int value1;
		char value1;
	};
		union
	{
		double value2;
		int value2;
		char value2;
	};
	cout << "Please enter first value";
	cin >> value1;

	cout << "Please enter second value";
	cin >> value2;
	result = IsEqual(value1,value2);
	if(result == 1)
	{
		cout << "The inputs match";
	}
	else
	{
		cout << "The inputs do not match";
	}
}
Last edited on
1) Your IsEqual functions returns void so your return 1 and others are meaningless. Change return type to int.

2) You are accepting pointers to template type T as arguments. Change to pass-by-value (const T input1) or to pass-by-reference (const T& input1)
Last edited on
thanks for the help.

i've changed the const t* to const t and changed it to int but i get alot of errors when trying to run it.

Error 1 error C2371: 'main::<unnamed-tag>::value1' : redefinition; different basic types

Error 2 error C2371: 'main::<unnamed-tag>::value1' : redefinition; different basic types

Error 3 error C2371: 'main::<unnamed-tag>::value2' : redefinition; different basic types

Error 4 error C2371: 'main::<unnamed-tag>::value2' : redefinition; different basic types

Error 5 error C2088: '>>' : illegal for class

Error 6 error C2088: '>>' : illegal for class

Error 7 error C2143: syntax error : missing ',' before ';'

Error 8 error C2143: syntax error : missing '>' before '{'

Error 9 error C2143: syntax error : missing ';' before '{'

Error 10 error C2143: syntax error : missing ';' before '}'

Error 11 error C2143: syntax error : missing ';' before '{'

Error 12 error C2143: syntax error : missing ';' before '}'

Error 13 error C2143: syntax error : missing ';' before '}'

Error 14 error C2143: syntax error : missing ';' before '}'

Error 15 error C1004: unexpected end-of-file found

16 IntelliSense: expression must have a constant value

17 IntelliSense: this operator is not allowed in a constant expression

18 IntelliSense: expression must have a constant value

19 IntelliSense: expected a '>'
You get unions wrong: http://en.cppreference.com/w/cpp/language/union
You still should specify value type (by specifying variable name) when you use them.
wow major brainfart on my end guys i totally misread what i needed to do i actually just need to allow people to compare each variable separately so ive redone my program but thanks for the help anyway. im quite glad i don't have to mess with unions just yet
Topic archived. No new replies allowed.