Converting a class to a template.

hvigil (41)
So I have my class done all I nee is to convert it to a template to be able to finish but I'm having trouble. Can anybody show me what to fix.
Thanks.

My Container class
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
#ifndef CONTAINER_H
#define CONTAINER_H

#include "item.h"
#include<iostream>
using namespace std;

template<class T>
class Container
{
private:
	void RemoveIndex(T id);
	T items[100];
	T counter;
	T num; /////// the number of items in the array.
public:

	void add_item(T New); /// Item New
	void size();
	void show();
	T remove(T id); // int id 
	Container();   ////// constructor 


};

#endif 



My container 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "container.h"
#include "item.h"
#include<iostream>
#include<string>


using namespace std;

int i;
template <class T>
T Container<T>::Container()
{

	counter = 0;

}
template <class T>
void Container<T>::add_item(T New)
{
	items[counter] = New;
	counter++;

}
template <class T>
void Container<T>::show()
{
	for ( int i = 0; i < counter; i++ )
	{
		cout<<items[i].toString()<<endl;

	}

}
template <class T>
void Container<T>::size()
{

	num = counter;

	cout<<" number of items : " <<num<<endl;

	

}
template <class T>
bool Container<T>::remove(T id)
{
	for ( int i =0 ; i <counter ; i++ )
	{
		if ( items[i].getId() == id )
		{
			RemoveIndex (i);
			return true;
		}
	}
	return false;
}
template <class T>
void Container<T>::RemoveIndex( T index )   /////// private
{
	for ( int i = index; i < counter; i++ )
	{
		items[i] = items[i + 1 ];
	}
	counter--;

}


And my main.

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
#include "auto.h"
#include "item.h"
#include "container.h"


int main()
{
	Container c;

	// quick test for Item class I wrote, delete once you
	//  get how it works	
	Item i( 1000, "White bread", 0.99 );
	Item y( 1993, "Dozen Eggs", 2.50 );
	Item x( 2013, "cookies" , 3.00 );
	c.add_item(i);
	c.add_item(y);
	c.add_item(x);

	c.show();
	c.remove(1993);
	c.show();
	// you'll use Auto in part 2, here's a quick example as well
	/*
	Auto a( 34, "Toyota", "Sienna", 2010 );
	cout << a.toString() << endl;
	*/
	

	system("pause");
	return 0;
}


I know the main is wrong
Registered users can post here. Sign in or register to post.