Bad Pointer for a list iterator

Just like the topic heading sounds, whenever I debug my code, my iterator is a "<Bad Ptr>". This only happens in the method where I am trying to write my HashTable to a file. In every other method the iterator works fine. Anyone know what's up?

Here is the method with the problem. The iterator in the for loop is the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 void HashTable::logFile(){
	string fileName;
	ofstream myfile;
	list<string>::iterator it;
	cout<<"Please enter file name: ";
	cin >> fileName;//gets file name
	myfile.open(fileName.c_str(), ios::out | ios::trunc);
	//cout<<myfile.is_open()<<endl;//shows if file opened or not
	int bucket = 0;
	for(it = l[bucket].begin(); it!=l[bucket].end(); it++){
		myfile<<" Bucket: "<<bucket<<" "<<endl;
		for(int j = 0; j < l[bucket].size(); j++){
		myfile<<"Name: " <<j + 1<<" "<<*it<<"\r\n";//print info
		}
		bucket++;
	}
	myfile.close();//close the file
}


In case you need it, here is the h file for my hash table.
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
#ifndef HASHTABLE_H
#define	HASHTABLE_H
#include <list>
#include <iostream>
#include <string.h>
using namespace std;

class HashTable {

public:
	HashTable();
	HashTable(int size);
	~HashTable();
	void setHash(bool whichHash);
	void insert(string name);
	void delName(string name);
	bool search(string name);
	void logFile();
	long hashFunction(string name);
	unsigned long hash(string name);
	unsigned long sdbm(string name);
private:
	int size;
	int numEntries;
	int numFull;
	double loadfactor;//num entries divided by num of buckets(index) i.e. 80 entries/ a table size of 10 = 8
	list<string> *l;
	bool whichHash;

};
#endif
1
2
3
4
for(it = l[bucket].begin(); it!=l[bucket].end(); it++){
    /*...*/
    bucket++;
}
On first iteration everything is fine, then you are comparing iterators from two different containers. Not to mention that there is serious loghic problem here.

1
2
3
for(int j = 0; j < l[bucket].size(); j++){
    myfile<<"Name: " <<j + 1<<" "<<*it<<"\r\n";//print info
}
You want to outpun single element n times? I think not, check logic here too.
Topic archived. No new replies allowed.