Bogdan
Bogdan

Reputation: 1

c++ debug assertion failed

I have a problem when I try to run this code

void MyClass::setUp(){
    list->clear();
    Iterator i = ctr.getAll().iterator();
    while (i.valid()) {
        list->addItem(QString::fromStdString(i.elem().getNr()));
        i.next();
    }
}

When I exit the function an error occurs:

Debug Assertion Failed!

File:C:\Program Files \Microsoft Visual Studio 14.0\ VC\ include\ xmemory0 line:106

Expression: "(_Ptr_user&(_BIG_ALLOCATION_ALIGNMENT -1))==0 " &&0

I am trying to iterate through a custom list

The Iterator class is this:

void Iterator::first(){
    while (pos < ht.nrElem && ht.list[pos] == nullptr)
        pos++;
    if (pos < ht.nrElem)
        current = ht.list[pos];
}

Iterator::Iterator(const HashTable & ht): ht { ht }{
    pos = 0;
    first(); // current will refer the first element of the list
}


void Iterator::next(){
    current = current->getNext();
    if (current == nullptr) {
        pos++;
        first();
    }
}

bool Iterator::valid(){
    return (pos < ht.nrElem) && (current != nullptr);
}

Car& Iterator::elem() const{
    return current->getCar();
}

Upvotes: 0

Views: 920

Answers (1)

user31264
user31264

Reputation: 6727

For one thing, Iterator::first and thus the constructor do not neccessarily initialize current. Second, in Iterator::Iterator(const HashTable & ht): ht { ht } does it mean that Iterator has a member ht , and the parameter is also called ht? It is a bad practice. Never call parameters of member functions the same name as class fields.

Upvotes: 0

Related Questions