new with templates but this may be a non-template issue

I have the following code:

header 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>

using namespace std;

namespace list
{
	template<class class_type>
	class List
	{
	public:
		List(short max = 0)
		{
			item = new class_type[max];
			current_length = 0;
		};

		~List()
		{
			delete [] item;
		};

		short length() const
		{
			return current_length;
		};

		void add(class_type new_element)
		{
			item[current_length] = new_element;
			current_length++;
		};

		void erase()
		{
			current_length = 0;
		};

		short find(class_type look_for)
		{
			short i(0);
			while(item[i] != look_for && i < max_length)
			{
				i++;
			}
			if(item[i] == look_for)
			{
				return i;
			}
			else//look_for is not in the list
			{
				return -1;
			}
		};

		friend ostream& operator <<(ostream& outs, const List<class_type>& list)
		{
			for(short i = 0; i < list.length(); i++)
			{
				outs << list.item[i] << endl;
			}
			return outs;
		}
	private:
		class_type* item;
		short max_length;
		short current_length;
	};
}

#endif 


application 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
#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <string>
#include "Array.h"

using namespace std;
using namespace list;

int main()
{
	
	List<double>list(10);
	list.add(1.1);
	list.add(5.5);
	list.add(2);
	cout << list;
	cout << "The number is in index: " << list.find(5.5) << endl;

	char exit;
	cout << "Enter character to exit: ";
	cin >> exit;

	return 0;
}


For some reason when I compile this code line 18 tells me that 5.5 is in index -1 (-1 is the value that the function "find" is supposed to send if it doesn't encounter the element). 5.5 is obviously in the list so it should return [positive] 1. What's the problem? I'm new with templates so perhaps it's something I don't know about yet.
Well, equality comparisons with floating point numbers are highly unreliable. My guess would be that there's some weird behavior going on behind the scenes involving the FPU and conversions between floating points types of different sizes. See what happens if you change the comparison to abs(item[i]-look_for)<0.0001.
helios,

Thanks for the advice but that's not the problem. I made the following changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	
	List<short>list(10);
	list.add(1);
	list.add(5);
	list.add(2);
	cout << list;
	cout << "The number is in index: " << list.find(2) << endl;

	char exit;
	cout << "Enter character to exit: ";
	cin >> exit;

	return 0;
}


and find still returned -1.
You don't initialize max_length in the constructor.
1
2
3
4
		short find(class_type look_for)
		{
			short i(0);
			while(item[i] != look_for && i < max_length)
¿what is the value of max_length ?
Actually, max_length should probably be current_length, there.
Thank you everyone. That was a bit embarrassing.
Topic archived. No new replies allowed.