I'm trying to scan data from a txt file line by line. I have a function that scans the whole file into a local generic linked list, after returns with the list. (by value). I have a generic linked list in my main function that gets the list returned by the function. Here starts the problems. My list class has explicit dtor, if I remove this dtor (ofc its memory leak), the whole thing works, but if is use this dtor, I get an error
Read access violation _Pnext was 0xDDDDDDE1
I don't know how can be the problem around the dtor. Maybe does it called somewhere where shouldn't?
I tried to construct the list in different way in the function and return with pointer but it didn't help.
This code is not my whole project, just the important things i hope.
class Card {
private:
string name1;
string name2;
string city;
string job;
string number;
string email;
public:
Card() {}
Card(string name1, string name2, string city, string job, string number,
string email);
~Card();
};
template <class L>
class MyList {
private:
struct Node {
Node* next;
L data;
~Node() {}
};
Node* head;
public:
MyList() { this->head = NULL; }
~MyList() {
Node* current = head;
while (current != NULL) {
Node* next = current->next;
delete current;
current = next;
}
head = NULL;
}
void add(const L& li) {
Node* last = new Node;
last->data = li;
last->next = head;
head = last;
}
/*class iterator { ... }
iterator begin() {}
iterator end() {}
*/
};
MyList<Card> scan(string name){
MyList<Card> list;
ifstream file(name);
if (file.is_open()) {
string line;
while (getline(file, line)){
string name1;
string name2;
string city;
string job;
string number;
string email;
istringstream iline(line);
getline(iline, name1, ',');
getline(iline, name2, ',');
getline(iline, city, ',');
getline(iline, job, ',');
getline(iline, number, ',');
getline(iline, email, ',');
Card c(name1, name2, city, job, number, email);
list.add(c);
}
file.close();
}
return list;
}
int main()
{
string filename;
MyList<Card> list;
cin >> filename;
list = scan(filename);
return 0;
}