为什么我会收到_Block_Type_Is_Valid(pHead->nBlockUse)错误?
Why do I get the _Block_Type_Is_Valid (pHead->nBlockUse) error?
我不知道为什么我一直得到_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;
。
相关文章:
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 在函数中删除第一个元素后,如何更新链接列表的phead
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 调试断言失败 BLOCK_TYPE_IS_VALID (pHead->nblockuse) 从解构函数
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>