Using structs in templates?

I'm really confused since I am totally new with templates.
I have it display a list of numbers already, but now I need to display a list of structs (in my code I have addressNumber, cost, and state). I need to list them.
I'm just confused on just adding it all.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

#include <iostream>
using namespace std;

struct houses
{
	int addressNumber;
	int price;
	string state;
};

template <class T>
class list
{
 private:
  #define MAXSIZE 50
	T ar[MAXSIZE];
	int number;
 public:
	list();
	bool isFull();
	bool isEmpty();
	void add(T x);
	void display();
	T remove();
};

template <class T>
list<T>::list()
{
	number = 0;
}

template <class T>
bool list<T>::isEmpty()
{
	return (number == 0);
}

template <class T>
bool list<T>::isFull()
{
	return (number == MAXSIZE);
}

template <class T>
void list<T>::add(T x)
{
	ar[number] = x;
	number++;
}

template <class T>
T list<T>::remove()
{
	number--;
	return ar[number];
}

template <class T>
void list<T>::display()
{
	for (int i = 0; i < MAXSIZE; i++)
	{
	cout << ar[i] << endl;
	}
}

/*int main()
{
	list <int> numList;
	numList.add(0);
	numList.add(1);
	numList.add(2);
	numList.add(3);
	numList.add(4);
	numList.add(5);
	numList.add(6);
	numList.add(7);
	numList.add(8);
	numList.add(9);
	numList.add(10);
	numList.add(11);
	numList.add(12);
	numList.add(13);
	numList.add(14);
	numList.add(15);
	numList.add(16);
	numList.add(17);
	numList.add(18);
	numList.add(19);
	numList.add(20);
	numList.add(21);
	numList.add(22);
	numList.add(23);
	numList.add(24);
	numList.add(25);
	numList.add(26);
	numList.add(27);
	numList.add(28);
	numList.add(29);
	numList.add(30);
	numList.add(31);
	numList.add(32);
	numList.add(33);
	numList.add(34);
	numList.add(35);
	numList.add(36);
	numList.add(37);
	numList.add(38);
	numList.add(39);
	numList.add(40);
	numList.add(41);
	numList.add(42);
	numList.add(43);
	numList.add(44);
	numList.add(45);
	numList.add(46);
	numList.add(47);
	numList.add(48);
	numList.add(49);
	numList.add(50);
	numList.remove();
	numList.display();
	system("pause");
	return 0;
}*/

int main()
{
	list <houses> myList;
	houses a, b, c;

	a.addressNumber = 400;
	a.price = 50000;
	a.state = "Michigan";
	b.addressNumber = 248;
	b.price = 35000;
	b.state = "Wyoming";
	c.addressNumber = 4573;
	c.price = 172352;
	c.state = "Soviet Union";

	myList.add(a);
    myList.add(b);
    myList.add(c);

	system("pause");
	return 0;
}
You will have to create a friend global overload function for the iostream << operator to display a
houses structure.

That was hard to say - what I mean is a function with this signature - (and if houses have any private members to diaplay you will have to make it a friend of houses

ostream& operator << (ostream & os, houses & h);


On another note - I assume that you will be putting in some error checking for the add and remove functions (to prevent array out of bounds access crashes)
Last edited on
Topic archived. No new replies allowed.