Using Templates, how to?

This is my .h 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
#ifndef CONTAINER3_H
#define CONTAINER3_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "item.h"
#include "auto.h"

template <class part2>
class Container3
{
public: 
	part2 Container3();
	void add_item(part2 item);
	part2 numberofitems();
	part2 displayitems();
	void removeitems(part2 id);

private:
	part2 _items[100];
	part2 _count;

};
#endif

template <class part2>
part2 Container3<part2>::Container3()
{
	_count=0;
}

template <class part2>
void Container3<part2>::add_item(part2 item)
{
	_items[_count]= item;
	_count++;
	
	
}
template <class part2>
part2 Container3<part2>::displayitems()
{
	for (int i=0; i < _count; i++)
	{
		 cout<<_items[i].toString()<<endl;
		
	}
}
template <class part2>
void Container3<part2>::removeitems(part2 id)
{
	for (int i=0; i < _count; i++)
	{
		if 	(id==_items[i].getId())
		{
		_items[i] = _items[i+1];
		_count--;
		}
	}
}
template <class part2>
part2 Container3<part2>::numberofitems()
{
 cout<<endl<<"Number of items equals: "<<_count<<endl;
}


and my main.cpp file...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

#include "auto.h"
#include "item.h"
#include "container.h"
#include "container2.h"
#include "container3.h"

template <class part2>;
using namespace std;
int main()
{

	//Container3 <Auto> c; ... both Auto and Item are classes
	Container3<Item> c;



	system("PAUSE");
	return 0;
}


I'm having trouble understanding how to use the methods using the template, please help.
Last edited on
why you use

template <class part2>;

top of using namespace std;

template has for classes or functions that we don't know type of it
like vector class it's good way for Software Engineering and reuse your app in other projects

Last edited on
You've defined your container class, but haven't shown us your Item class.
In main, you would define an Item, then add it to your container.

Something like this:
1
2
3
4
5
6
7
8
9
int main()
{   Item a;
     Container3<Item> c;

     // Initialize a
     c.add_item(a);	
     c.display();
     return 0;
}
so i defined the item followed by defining the the container template or the template container (not sure how to say it) like so...

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
#include <iostream>
#include <string>

#include "auto.h"
#include "item.h"
#include "container.h"
#include "container2.h"
#include "container3.h"


using namespace std;
template <class part2>;
int main()
{
	
	
	Item i( 1567, "White bread", .99 );


	Item i1( 1568, "Wheat bread", 1.99 );
	
	Item i2( 1569, "Italian bread", 2.99 );

	
	Container3<Item> c;
	c.add_item(i);

	system("PAUSE");
	return 0;
}


Still getting errors , can't figure it out....
Remove line 11.
Update... So now the program is accepting the Constructor calling, but when it comes to adding objects to the container I'm getting error...
main cpp.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
#include <iostream>
#include <string>

#include "auto.h"
#include "item.h"
#include "container.h"
#include "container2.h"
#include "container3.h"

using namespace std;
//template <class param>;

int main()
{

	
	Item i( 1567, "White bread", .99 );
	cout << i.toString() << endl;


	Item i1( 1568, "Wheat bread", 1.99 );
	cout << i1.toString() << endl;


	Item i2( 1569, "Italian bread", 2.99 );
	cout << i2.toString() << endl;
	
	
	//adding new Item object to the container
	
	Container3<Item> c();
	c().add_item ( i) ;

	system("PAUSE");
	return 0;
}
Line 22 of '.h' file: part2 _count;
line 36, 37, add_inem method:
1
2
	_items[_count]= item;
	_count++;


It translates when created with <Item> template parameter to:
Item _count;
then you are trying to use Item as an array index, and thet increment it.
I see now , so if I give _count a constant type of int. and change my main code to
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
#include <iostream>
#include <string>

#include "auto.h"
#include "item.h"
#include "container.h"
#include "container2.h"
#include "container3.h"

using namespace std;
//template <class param>;

int main()
{
	
	Item i( 1567, "White bread", .99 );
	cout << i.toString() << endl;


	Item i1( 1568, "Wheat bread", 1.99 );
	cout << i1.toString() << endl;


	Item i2( 1569, "Italian bread", 2.99 );
	cout << i2.toString() << endl;
	
	
	//adding new Item object to the container
	
	Container3<Item> c;
	c.add_item ( i) ;

	


	system("PAUSE");
	return 0;
}


do you agree?
Topic archived. No new replies allowed.