为什么我会收到_Block_Type_Is_Valid(pHead->nBlockUse)错误?

Why do I get the _Block_Type_Is_Valid (pHead->nBlockUse) error?

本文关键字:gt pHead- nBlockUse 错误 Valid Block Is Type 为什么      更新时间:2023-10-16

我不知道为什么我一直得到_Block_Type_Is_Valid(pHead->nBlockUse)错误。我知道这通常是因为我在双重删除一些东西,但我只在代码中使用了一次删除。以下是代码。

Box.h

#ifndef BOX_H_
#define BOX_H_
//containing the box methods
class Box {
public:
Box();
void setValue(int Value);
void setPrevious(Box* prev);
int getValue();
    Box* getPrevious();
private:
    int m_value;
    Box* m_previous;
};
#endif /* BOX_H_ */

StackOfBoxes.h

#ifndef BOX_H_
#define BOX_H_
//containing the box methods
class Box {
public:
Box();
void setValue(int Value);
void setPrevious(Box* prev);
int getValue();
Box* getPrevious();
private:
int m_value;
Box* m_previous;
};
#endif /* BOX_H_ */

Box.cpp

#include "Box.h"

//containers
Box::Box()
{
m_previous=nullptr;
int m_value = 0;
}
void Box::setPrevious(Box* prev)
{
m_previous = prev;
}
void Box::setValue(int val)
{
m_value = val;
}
int Box::getValue()
{
return m_value;
}
Box* Box::getPrevious()
{
return m_previous;
}

Main.cpp

#include <iostream> //std::cout std::cin
#include "StackOfBoxes.h" //StackOfBoxes
int main()
{
StackOfBoxes stack; //Create an empty stack allocated stack
StackOfBoxes* stackPtr = new StackOfBoxes(); //Create a heap allocated stack
int sizeOfStack;    //int we'll use later to store the size of the stack
//push some numbers onto the stack
for (int i = 1; i <= 10; i++)
{
    stack.push(i * 5);
    stackPtr->push(i * 5);
}

//Store the size of the stack before popping anything
sizeOfStack = stack.size();
std::cout << "There are " << sizeOfStack << " items on the stack" << std::endl;
//Think about why we don't use i<stack.size()
for (int i = 0; i < sizeOfStack; i++)
{
    std::cout << "Popping the top: " << stack.pop() << std::endl;
    //We won't pop anything from stackPtr
}
//Deleting a pointer calls the destructor for the object it points to
delete stackPtr;
}

最后但同样重要的是,我认为错误产生的代码StackOfBoxes.cpp

#include "StackOfBoxes.h"
#include <iostream>
//sets top and size
StackOfBoxes::StackOfBoxes()
{
m_top = nullptr;
m_size = 1;
}
//if top has a number it will pop
StackOfBoxes::~StackOfBoxes()
{
while (m_top != nullptr)
{
    pop();
}
}
//but if its empty it will stop popping
bool StackOfBoxes::isEmpty() const
{
if(m_size==0)
{
    return true ;
}
else
{
    return false ;
}
}
//how big is the stack
int StackOfBoxes::size() const
{
return m_size;
}
//pushes the top to reveal the next one
void StackOfBoxes::push(int val)
{
Box* temp = new Box();
temp->setValue(val);
temp->setPrevious(m_top);
m_top = temp;
m_size++;
}
//takes the top off of the stack
int StackOfBoxes::pop()
{
Box* temp = m_top->getPrevious();
int topp = m_top->getValue();
delete m_top;
m_size--;
return topp;
}

如有任何指导,我们将不胜感激。非常感谢。

int StackOfBoxes::pop()
{
Box* temp = m_top->getPrevious();
int topp = m_top->getValue();
delete m_top;
}

CCD_ 1被分配但从未被使用。CCD_ 2是一个悬空指针。你可能想在那里的某个地方做m_top = temp;