gpt4 book ai didi

c++ - 为什么会出现 _Block_Type_Is_Valid (pHead->nBlockUse) 错误?

转载 作者:行者123 更新时间:2023-11-28 06:41:09 25 4
gpt4 key购买 nike

我不知道为什么我总是收到 _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;
}

temp 已分配但从未使用过。 m_top 是一个悬空指针。您可能打算在某处执行 m_top = temp;

关于c++ - 为什么会出现 _Block_Type_Is_Valid (pHead->nBlockUse) 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25924895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com