Priority Queue out of bounds

I have a priority queue being used to store my bid objects for an item (class requirement). For some reason, I'm getting an error that says expression: vector iterator not dereferencable when it comes to printing my second item object (case 5 in switch on main program). This was working before I added the bid queue. It seems straightforward to me. I have no idea how this is happening.

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

// item class
#ifndef H_ITEMS
#define H_ITEMS
#include <iostream>
#include <string>
#include <list>
#include <queue>
#include <iomanip>
#include "customers.h"
#include "date.h"
#include "bids.h"
using namespace std;

class Item
{
public:
	Item();
	Item(int, int, int, int, int, int, int, 
		int, int, int, int, int, string, double);
	void setItem(int, int, int, int, int, int, int, 
		int, int, int, int, int, string, double, double, bool);
	int getItemIDd();
	string getItemName();
	double getPrice();
	bool getisSold();
	double getTotalCommission();
	double getCommission();
	double getHighestBid();

	void AddtoBids(Bid &);
	void itemSold(int, int, int, int, int, double, const double COM_PER);
	void findItem(int, bool &);
	bool BidisEmpty();
	void CalculateTotalCommission();

	void printCustomersItems();
	void printItemsForSale();
	void printItemsSold();
	void printItemsAndBiddingList();
	void isCustomerItem(int, bool &);

	//friend bool operator<(const Bid &, const Bid &);

private:
	int itemID;
	int cID;
	string itemName;
	double openingPrice;
	bool isSold;
	Date listDate;
	Date endDate;
	double commission;

	static double totalCommission;
	priority_queue <Bid> bidQueue;
};
#endif

Item::Item(int y, int m, int d, int h, int min, int ey, int em, int ed, int eh, int emin,
	int id, int cId, string i, double p):
	listDate(y, m, h, d, min), endDate(ey, em, ed, eh, emin)
{

	itemID = id;
	cID = cId;
	itemName = i;
	openingPrice = p;
	isSold = false;
	commission = 0;
}


void Item::setItem(int ly, int lm, int ld, int lh, int lmin, 
	int ey, int em, int ed, int eh, int emin, int id, int cId, string i, double p, double c, bool s)
{
	itemID = id;
	cID = cId;
	itemName = i;
	openingPrice = p;
	listDate.setDate(ly, lm, ld, lh, lmin);
	isSold = s;
	commission = c;
	cout << "set Item commission " << commission << endl;
	endDate.setDate(ey, em, ed, eh, emin);
}

// bids are added from a separate file. I've checked this and it works

void Item::AddtoBids(Bid &temp)
{
	cout << temp.getItID() << " " << temp.getBid() << endl;
	bidQueue.push(temp);
}


//   Here is the print function that gives me the error

void Item::printItemsForSale()
{
	Bid sTemp = bidQueue.top();
	cout << "Item ID: " << itemID << endl;
	cout << "Name of item: " << itemName << endl;
	cout << fixed << setprecision(2);
	cout << "Opening Price: $" << openingPrice << endl;
	cout << "Current highest bid: $" << sTemp.getBid() << endl;
	
}

// bid class

#ifndef H_BIDS
#define H_BIDS
#include<iostream>
#include<queue>
#include "date.h"
using namespace std;

class Bid
{
public:

	Bid();
	Bid(int, int, int, int, int, int, int, double);
	void setBid(int, int, double, int, int, int, int, int);
	int getItID();
	int getcustID();
	double getBid();
	friend bool operator<(const Bid &, const Bid &);

private:
	int itID;
	int custID;
	double bid;
	Date bidDate;

};
#endif

Bid::Bid()
{
	itID = 0;
	custID = 0;
	bid = 0;
	bidDate.setDate(0, 0, 0, 0, 0);
}

Bid::Bid(int y, int m, int d, int h, int min, int i, int c, double b):
	bidDate(y, m, d, h, min)
{
	itID = i;
	custID = c;
	bid = b;
}

void Bid::setBid(int i, int c, double b, int y, int m, int d, int h, int min)
{
	itID = i;
	custID = c;
	bid = b;
	bidDate.setDate(y, m, d, h, min);
}


double Bid::getBid()
{       return bid;       }

bool operator<(const Bid &right, const Bid &left)
{
	return  (right.bid < left.bid);
}

#include <iostream>
#include <string>
#include <list>
#include <iomanip>
#include <fstream>
#include<queue>
#include "customers.h"
#include "date.h"
#include "items.h"
#include "bids.h"
using namespace std;

void createItemList(fstream&, list<Item> &);
void createCustomerList(fstream&, list<Customer> &);
void createBidQueue(fstream&, list<Item> &, list<Item>::iterator &, list <Bid> &, list <Bid>::iterator &);

void findCustomer(int cid, list<Customer> &, list<Customer>::iterator &, bool &); 
void findItem(int id, list<Item> &, list<Item>::iterator &, bool &); 

void enterNewCustomer(list<Customer> &, list<Customer>::iterator &);
void enterNewItem(list<Item> &, list<Item>::iterator &, list<Customer> &, list<Customer>::iterator &);
void enterNewBid(list<Item> &, list<Item>::iterator &, list<Customer> &, list<Customer>::iterator &);

void enterCustomerID(int &);
void enterTime(int &, int &, int &, int &, int &, int &, int &, bool &);
void displayMenu();

const double COM_PER = 0.1;
double Item::totalCommission = 0;

int main()
{
	list <Item> itemList;
	list <Item>::iterator i;
	list <Customer> customerList;
	list <Customer>::iterator c;
	list <Bid> bidList;
	list <Bid>::iterator b;

	char ch;
	int choice, cid;          
	bool found;

    fstream itemfile, custfile, bidfile;

            //open the customers file
    custfile.open("customers.txt");
    if (!custfile)
    {
        cout << "The customer file does not exist. "
             << "This program will terminate." << endl;
//		system ("pause");
        return 1;
    }

        //create the Customer list
    createCustomerList(custfile, customerList);


          //open the items file
    itemfile.open("bidItems.txt");
    if (!itemfile)
    {
        cout << "The items for bidding file does not exist. "
             << "This program will terminate." << endl;
        return 1;
    }
				//create the Bid list
	createItemList(itemfile, itemList);

         //open the bids file
    bidfile.open("bids.txt");
    if (!bidfile)
    {
        cout << "The bidding file does not exist. "
             << "This program will terminate." << endl;
        return 1;
    }
				//create the Item list
	createBidQueue(bidfile, itemList, i, bidList, b);

			// Start of menu
	displayMenu();
    cout << "Enter your choice: ";
    cin >> choice;    
    cin.get(ch);
    cout << endl;

        //process the requests
    while (choice != 9)
    {
 		bool cfound = false, customerItem=false;
		int x = 0, y = 0;
       switch (choice)
        {
		case 1:		//enter new customer
			enterNewCustomer(customerList, c);
			break;

		case 2:		//enter new item
			enterNewItem(itemList, i, customerList, c);
			break;
			
		case 3:  //change to add a bid

			enterNewBid(itemList, i, customerList, c);
			break;

			// need a case to end bidding

        case 4:	  //list items for sale of a particular customer
			do
			{
				cout << "\nEnter Customer ID of seller: ";
				cin >> cid;
				findCustomer(cid, customerList, c, cfound);
				if (!cfound)
				{
					cout << "Customer ID is not valid.\n";
					x++;
				}
			}while (!cfound && (x < 3));

			if (cfound)
			{
				c->printCustomer();
				for (i=itemList.begin(); i!=itemList.end(); ++i)
				{
					i->isCustomerItem(cid, customerItem);
					if(customerItem)
					{
						if (!(i->getisSold()))
						{
							i->printCustomersItems();
							y++;
						}
					}
				}
			}
			else
				cout << "Too many errors.\n\n";

				if ((cfound) && (y == 0))
					cout << "Customer has no items for sale.\n\n";
	
			break;

		case 5:		// print all items for sale
			
			for (i=itemList.begin(); i!=itemList.end(); ++i)
			{
				if(!(i->getisSold()))        //exclude sold items
					i->printItemsForSale();
			}
			break;


		case 6:    // print all items sold

			for (i=itemList.begin(); i!=itemList.end(); ++i)
			{
				if(i->getisSold())
	 			    i->printItemsSold();
			}
			break;

		case 7:   // list of items with their bids

			for (i =itemList.begin(); i!=itemList.end(); ++i)
			{
				i->printItemsAndBiddingList();
					
			}

		default:
			cout << "Invalid selection.\n\n";
		}	// end switch

		if (choice != 9)
		{
			displayMenu();     //display menu
			cout << "Enter your choice: ";
			cin >> choice;     //get the next request
			cin.get(ch);
			cout << endl;
		}	// end switch

	}		// end while


	custfile.close();
    itemfile.close();
	bidfile.close();
	system("pause");
	return 0;
}
Nevermind, I just found it. Forgot to check if the priority queue was empty.
Topic archived. No new replies allowed.