Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么会出现_Block _Type _isvalid(pHead->;nBlockUse)错误?_C++_Visual Studio_Crash - Fatal编程技术网

C++ 为什么会出现_Block _Type _isvalid(pHead->;nBlockUse)错误?

C++ 为什么会出现_Block _Type _isvalid(pHead->;nBlockUse)错误?,c++,visual-studio,crash,C++,Visual Studio,Crash,我不知道为什么我总是得到_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 ge

我不知道为什么我总是得到_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_ */
#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_ */
stackofbox.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_ */
#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;
}
#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;
}
#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;
}
Main.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;
}
#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;
}
#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;
}
#包括//std::cout std::cin
#包括“StackOfBox.h”//StackOfBox
int main()
{
StackOfBox堆栈;//创建空堆栈分配堆栈
StackOfBox*stackPtr=new StackOfBox();//创建一个堆分配的堆栈
int-sizeOfStack;//我们稍后将使用int来存储堆栈的大小
//将一些数字推到堆栈上
对于(int i=1;i推动(i*5);
}
//在弹出任何内容之前存储堆栈的大小
sizeOfStack=stack.size();
std::cout getValue();
删除m_top;
m_尺寸--;
返回顶部;
}
非常感谢您的指导,谢谢

int StackOfBoxes::pop()
{

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

temp
已分配但从未使用。
m_top
留下了一个悬空的指针。你可能想在那里的某个地方执行
m_top=temp;

为什么
Box.h
stackofbox.h
都定义了
Box
但我在代码中只使用了一次delete;
这不是你看到的次数
delete
,这是您调用
delete
的次数。不仅是次数,还包括您是否使用有效的指针值调用它。太棒了!修复了它。非常感谢您发现我的错误!