Help converting a class to a template

Hello I'm having trouble converting a class to a template. It gives me two errors when I compile it. I cannot find whats wrong , but I'm pretty sure I did not write the template correctly.

This is my 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
#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 


This is my class 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
#include "container.h"
#include "item.h"
#include<iostream>
#include<string>

using namespace std;

template <class 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>
T 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 Finally 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
32
33
34
#include <iostream>
using namespace std;

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

	Container<Item> shopping_cart;

int main()
{
	


	// 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 );
	

	shopping_cart.add_item(i);
	

	// 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;
}
What are the errors?

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T>
T Container<T>::remove(T id)
{
	for ( int i =0 ; i <counter ; i++ )
	{
		if ( items[i].getId() == id )
		{
			RemoveIndex (i);
			return true;
		}
	}
	return false;
}

This is wrong, you have the return type as T, but you're returning bools from the function.
Templates look fine, but without knowing what the actual error messages are I can't help you much

EDIT:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T>
T Container<T>::remove(T id)
{
	for ( int i =0 ; i <counter ; i++ )
	{
		if ( items[i].getId() == id )
		{
			RemoveIndex (i);
			return true;
		}
	}
	return false;
}

id is not the same type as the container is holding. It's a type that's defined in T. I assume ID is an int type, so I'd change that. And also, this container can now only be used on types that have a gedId() method.
Last edited on
The implementation has to be available when the template is instantiated, so if you put the implementation in the header it should work better.
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Container<class Item>::add_item(class Item)" (?add_item@?$Container@VItem@@@@QAEXVItem@@@Z) referenced in function _main


1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Container<class Item>::Container<class Item>(void)" (??0?$Container@VItem@@@@QAE@XZ) referenced in function "void __cdecl `dynamic initializer for 'shopping_cart''(void)" (??__Eshopping_cart@@YAXXZ)

1>C:\Users\Heriberto\Desktop\hw\Debug\container.exe : fatal error LNK1120: 2 unresolved externals

these are my errors
Last edited on
Topic archived. No new replies allowed.