mercredi 1 février 2017

Access Violation Writing Location Stack Overflow error C++

I was given a project to fix as a way of getting a taste of C++. The basic concept is to compare a book category with the current category, and if it matches, add it onto the stack, but otherwise, don't.

This is the code that I've got:

StoragePile::StoragePile(string categoryB) {
PileCategory = categoryB;
}

bool StoragePile::AddBook(Book b) {
    if (b.category == PileCategory) {
        AddBook(b);
        return true;
    }
    else {
        cout << "Book is not of the same category as this storage pile." << endl;
        return false;
    }
}

Stepping through it shows me that it's continually iterating over the lines if (b.category == PileCategory) { and AddBook(b);, and never returns to Tru or False. Eventually, it gives off 2 errors:

Unhandled exception at 0x0F985B2D (ucrtbased.dll) in Practical attempt.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00802FFC).

Followed by:

Exception thrown at 0x77885D28 (ntdll.dll) in Practical attempt.exe: 0xC0000005: Access violation writing location 0x00800FA8.

If there is a handler for this exception, the program may be safely continued.

Why is this happening? What is the fix for it?

Aucun commentaire:

Enregistrer un commentaire