Templates

Hi can someone help me finish the rest of this code?

functions.h
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
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__

#include <iostream>
#include <string>

using namespace std; 

template <class T> 
void myswap(T& a, T& b) 
{
  T c = a;
    a = b; 
    b = c;

}

template <class T>
T min (T& a, T& b) 
{
	if (a < b) 
	{ 
          return a;
	} 
	else 
	{ 
	  return b;
	}
}

template <class T> 
T sum(T *arr, int ARRLEN) 
{	T sum = 0; 
	for (int i = 0; i < ARRLEN; i++)
		sum += arr[i];

return sum;
}

template <class T>
T count(T *arr, T value, int ARRLEN)
{
	int count = 0;
	int i = 0;
	for (i=0; i < ARRLEN; i++)
	{
		if(arr[i] == value)
		{
			++count;
		}	
	}
return count; 

}

#endif 


main.cpp
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>
#include <assert.h>
#include "functions.h"

using namespace std; 

#define ARRLEN 6

int main(int argc, char *argv[])
{
    
    int x, y; 
    double a, b;

    int iarr[] = {42, 42, 42, 56, 65, 99};
    float farr[] = {3.14, 3.14, 3.2, 4.5, 7.8, 0.0};
    
    x = 6; 
    y = 42; 

    a = 3.14159; 
    b = 3.14160; 
    
    assert(min(x,y) == x); 
    assert(min(a,b) == a);
    
    assert(count(iarr, y, ARRLEN) == 3);
    assert(count(farr, 3.14f, ARRLEN) == 2);

    assert(sum(iarr, ARRLEN) == 346);   
    assert(sum(farr, ARRLEN) == 21.78f);
    
    // Can't use *swap* because it's part of the STL
    myswap(x, y); 
    assert(x == 42);
    assert(y == 6); 
    
    myswap(a, b); 
    assert(a == 3.14160); 
    assert(b == 3.14159); 
    
    cout << "All tests passed!" << endl;

}



I get this error:
1
2
3
4
5
6
7
8
9
10
In file included from main.cpp:3:
functions.h:19: error: redefinition of ‘template<class T> T sum(T&, T&)’
functions.h:14: error: ‘template<class T> T sum(T&, T&)’ previously declared here
functions.h:29: error: expected ‘,’ or ‘...’ before numeric constant
main.cpp: In function ‘int main(int, char**)’:
main.cpp:27: error: no matching function for call to ‘count(int [6], int&, int)’
main.cpp:28: error: no matching function for call to ‘count(float [6], float, int)’
main.cpp:30: error: no matching function for call to ‘sum(int [6], int)’
main.cpp:31: error: no matching function for call to ‘sum(float [6], int)’
make: *** [lab5] Error 1
Last edited on
ok then i got the code :D this is the code i developed: can someone let me know if its right?



Looks good. I only see one part that isn't quite right:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T>
T count(T *arr, T value, int ARRLEN)
{
	int count = 0;
	int i = 0;
	for (i=0; i < ARRLEN; i++)
	{
		if(arr[i] == value)
		{
			++count;
		}	
	}
return count; 

}

The return value should be the same type as the local variable count, not the template parameter T. Although it would appear to still work as it is, it is performing unnecessary implicit conversions to double (and back).

You tested it, right?
Yeah i did and it compiled and ran properly
Cool.
Awesome thank you so much for the guidance and help
Topic archived. No new replies allowed.