Reputation: 39
I was trying to fit list creation in a class, but ran into this run-time error called:
Debug Assertion Failed! Expression: list iterator not incrementable
I can't figure out what the problem is. Here's the code:
// Core.cpp //
#include "Core.h"
class List {
private:
int Size;
int Values;
list<int> Data;
list<int>::iterator IT;
public:
List(int S, int V) {Size = S; Values = V;}
void MakeList();
void TargetElement(int Element);
void ChangeElement(int Value);
void DisplayList();
};
void List::MakeList() {
list<int> Data(Size, Values);
list<int>::iterator IT = Data.begin();
}
void List::TargetElement(int Element) {
advance(IT, Element);
}
void List::ChangeElement(int Value) {
list<int>::iterator INSERT = Data.begin();
Data.insert(INSERT, Value);
}
void List::DisplayList() {
list<int>::iterator OUTPUT;
for(OUTPUT = Data.begin(); OUTPUT != Data.end(); OUTPUT++) {
//cout << *OUTPUT << endl;
}
}
int main() {
List Container(10, 0);
Container.MakeList();
Container.TargetElement(3);
Container.ChangeElement(20);
Container.DisplayList();
cin.get();
return 0;
}
Any suggestions on how I could fix this issue?
EDIT: I looked everywhere for an answer and I still can't seem to find the solution. Debugging and finding the break-point didn't help either.
Upvotes: 2
Views: 2740
Reputation:
The problem is here:
void List::MakeList() {
list<int> Data(Size, Values);
list<int>::iterator IT = Data.begin();
}
This doesn't initialize the Data and IT members of the class, it just creates local variables called Data and IT which disappear once MakeList returns. To initialize those members, change the constructor to:
List(int S, int V) : Data(S, V), IT(Data.begin()), Size(S), Values(V)
{
}
By the way, you don't need the Size member because std::list already stores its size (you can get it by calling std::list::size).
Upvotes: 1
Reputation: 122001
IT
is invalid within TargetElement()
because IT
it is an iterator obtained from the local variable Data
within MakeList()
as Data
is redeclared:
void List::MakeList() {
list<int> Data(Size, Values); // 'Data' is destructed when
list<int>::iterator IT = Data.begin(); // this method returns.
}
I am unsure why MakeList()
even exists, perform the initialization within the constructor:
List(int S, int V) : Size(S), Values(V), Data(S, V) {}
and remove MakeList()
. If you choose this approach note that Size
and Values
can be removed from the class as they are not required outside of construction.
Upvotes: 1
Reputation: 66234
For starters, this makes a local list<int>
called Data
, then a local iterator called IT
assigned to its first element. This has not doing anything to your member variables Data
and IT
whatsoever.
void List::MakeList() {
list<int> Data(Size, Values);
list<int>::iterator IT = Data.begin();
}
Should likely be:
void List::MakeList() {
Data.resize(Size, Values);
IT = Data.begin();
}
Run with that and see where it takes you.
Upvotes: 1
Reputation: 95355
void List::MakeList() {
list<int> Data(Size, Values);
list<int>::iterator IT = Data.begin();
}
This is creating two local variables that shadow the member variables of your class. You are getting the runtime error because the IT
member variable of your class is never set to an iterator of an actual list object. What you want is to merge MakeList
and your constructor. This is what constructors are for.
Upvotes: 0