Template Help

I am having problem with my template. It is not correctly working to find the maximum number, i believe this is a format mistake that can be corrected. I am using two files to create a c++ code using template that finds the maximum integer of a given array.

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
  //Purpose: To find the maximum
//Course: Comp 130
//Professor: Mia Chia


//Driver file.
#include<iostream>
#include <string>
#include <cmath>
#include "maximum.h"
using namespace std;
int main() {
	int arrA[3] = { 1 , 2000, 3 };
	double arrB[3] = { 45.7, 23.6, 999.6 };
	string arrC[3] = { "ghi", "abc","def" };



	maximum <int> intList;

	cout << "Maximum from integer array = " << maxiNum(arrA) << endl; // I HAVE AN UNDEFINED Identifier here

	return 0;
}

//maximum.h
#include <iostream>
using namespace std;
#include <string>
template<class T>
class maximum {

public:
	//maximum;
	T maxiNum();
	void displayList();
	int arrA[3] = { 1 , 2000, 3 };
	double arrB[3] = { 45.7, 23.6, 999.6 };
	string arrC[3] = { "ghi", "abc","def" };

private:
	int capacity = 5;
	int first = 0;
	int last = 0;

};
template<class T>
T maximum<T>::maxiNum() {  //This function should find the maximum of a given array
	T num1;
	T num2;
	T num3;
	T max;



	int arrA[3] = { 1 , 2000, 3 };
	double arrB[3] = { 45.7, 23.6, 999.6 };
	string arrC[3] = { "ghi", "abc","def" };

	num1 = arrA[0], arrB[0], arrC[0];
	num2 = arrA[1], arrB[1], arrC[1];
	num3 = arrA[2], arrB[2], arrC[2];

	num1 = max;


	if (num2 > num1) {
		num2 = max;
	}
	if (num3 > num2) {
		num3 = max;
	}

	return max;
}
template<class T>
void maximum<T>::displayList()
{
	for (int i = 0; i < 5; i++)
	{
		cout << arrA[i] << " ";
		cout << arrB[i] << " ";
		cout << arrC[i] << " ";
	}
}
You have defined a function maxiNum that takes no arguments, but in main you call it with one argument.

There shouldn't be a need to respecify the data in the class and in every function, just do it once only in main.Like wise there should be no need for variables num1, num2, num3. And the comma operator doesn't work like that in lines 60-62.

It would be better if you used a generic method for calculating the maximum, not restrict it to 3 values.

Don't put using namespace std; in header files. Try not to use it at all. Put std:: before each std thing.
Topic archived. No new replies allowed.