Template classes

Anyone know how to add a template class to my code below?

Got this so far with loads of errors

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>

using namespace std;

int intInput1, intInput2;
float floatInput1, floatInput2;
char charInput1, charInput2;

string stringInput1, stringInput2;

template <class T>
class Number
{
public:

void isEqual<T>(T intInput1, T intInput2)
{	
	if (intInput1 == intInput2)
	{
		cout << "Is equal" << endl;;
	}
	else
	{
		cout << "Not equal" << endl;
	}
}

void isEqual<T>(T floatInput1, T floatInput2)
{
	if ((float)floatInput1 == (float)floatInput2)
	{
		printf("is equal");
		cout << endl;
	}
	else
	{
		printf("not equal");
		cout << endl;
	}
}

void isEqual<T>(T charInput1, T charInput2)
{	
	if (charInput1 == charInput2)
	{
		cout << "Is equal" << endl;
	}
	else
	{
		cout << "Not equal" << endl;
	}
}

void isEqual<T>(T stringInput1, T stringInput2)
{
	if (stringInput1 == stringInput2)
	{
		cout << "is equal" << endl;
	}
	else
	{
		cout << "not equal" << endl;
	}
}
};

int main()
{
	Number<int> i;
	cout << "item comparison" << endl;

	cout << "Please enter 2 integers" << endl;
	cin >> intInput1 >> intInput2;
	isEqual(intInput1, intInput2);

	cout << "Please enter 2 floats" << endl;
	cin >> floatInput1 >> floatInput2;
	isEqual(floatInput1, floatInput2);

	cout << "Please enter 2 chars" << endl;
	cin >> charInput1 >> charInput2;
	isEqual(charInput1, charInput2);
	
	cout << "Please enter 2 strings" << endl;
	cin >> stringInput1 >> stringInput2;

	isEqual(stringInput1, stringInput2);

	cin.ignore(2);
}
Remove all the <T> after the IsEqual member functions - they are inside a templated class, they know they are templated already.

Also, you should only have ONE IsEqual function, unless you need to spcialize, but fix what I said first and remove all but one version of the function.

1
2
3
4
5
6
7
template<typename T>
struct MyClass
{
    void MyMemberFunction()
    {
    }
};
Try this:

header.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef __MYHEADER__
#define __MYHEADER

template <typename T>
class Number
{
public:

	static bool isEqual(T& inp1, T& inp2);
};

#endif 


source file:
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
#include "header.h"
#include <iostream>
#include <string>

using namespace std;

template <typename T>
bool Number<T>::isEqual(T& inp1, T& inp2)
{
	return inp1 == inp2;
}

void write_result(const bool result)
{
	if (result)
		cout << "They are equal" << endl;
	else
		cout << "They are not equal" << endl;
}

int main()
{
//	Number<int> i;
	cout << "item comparison" << endl;

	cout << "Please enter 2 integers" << endl;
	int intInput1, intInput2;
	cin >> intInput1 >> intInput2;
	write_result (Number<int>::isEqual(intInput1, intInput2));

	cout << "Please enter 2 floats" << endl;
	float floatInput1, floatInput2;
	cin >> floatInput1 >> floatInput2;
	write_result(Number<float>::isEqual(floatInput1, floatInput2));

	cout << "Please enter 2 chars" << endl;
	char charInput1, charInput2;
	cin >> charInput1 >> charInput2;
	write_result(Number<char>::isEqual(charInput1, charInput2));
	
	cout << "Please enter 2 strings" << endl;
	string stringInput1, stringInput2;
	cin >> stringInput1 >> stringInput2;
	write_result(Number<string>::isEqual(stringInput1, stringInput2));

	cin.ignore(2);

	return 0;
}
@ajh32 that is incorrect, functions in templated classes should be defined inline. It would work in this case, but if he wanted to use the class in any other source files he would have to copy the definition of the function. It is better to define member functions inline for templated classes.
Last edited on
If you want specialized template functions for specific types in the parameter, you have to do:

1
2
3
4
5
6
7
8
9
10
11
12
template<>
void isEqual(string stringInput1 , string stringInput2)
{
 if (stringInput1 == stringInput2)
 ...
}

template<>
void isEqual(float floatInput1, float floatInput2)
{
 ...
}

@yelnatz you forgot that you have to have <string> and <float> right after the function name for each specialization, respectively.
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
template<>
void isEqual<string>(string stringInput1 , string stringInput2)
{
 if (stringInput1 == stringInput2)
 ...
}

template<>
void isEqual<float>(float floatInput1, float floatInput2)
{
 ...
}
@yelnatz - that obviates the need for templates!
In any case, it only makes sense to specialize for floating point types, as using the == operator would not be acceptible - the normal templated function works fine for std::string and so does not need a specialization.
It does, but from his initial code he seems to be trying to do specific things given what the input types are.

I'm just letting him know how to do it properly, regardless if it makes templating useless in this case.
Topic archived. No new replies allowed.