C++ 将值放在堆栈顶部
我有一个程序,它应该获取一个值并将其放在顶部框中 stackOfBox.cppC++ 将值放在堆栈顶部,c++,memory-leaks,stack,C++,Memory Leaks,Stack,我有一个程序,它应该获取一个值并将其放在顶部框中 stackOfBox.cpp StackOfBoxes::StackOfBoxes() { m_top=nullptr; m_size=0; } bool StackOfBoxes::isEmpty() const { if (m_size==0) { return true; } return false; } int StackOfBoxes::size() const {
StackOfBoxes::StackOfBoxes()
{
m_top=nullptr;
m_size=0;
}
bool StackOfBoxes::isEmpty() const
{
if (m_size==0)
{
return true;
}
return false;
}
int StackOfBoxes::size() const
{
return m_size;
}
void StackOfBoxes::push(int value)
{
Box* Box1 = new Box;
Box1 -> m_previous = m_top;
m_top=Box1;
++m_size;
}
箱堆
class StackOfBoxes
{
private:
Box* m_top;
int m_size;
public:
StackOfBoxes();
bool isEmpty() const;
int size() const;
void push(int value);
int pop();
};
方框h
Box.cpp
Box::Box()
{
m_value=0;
m_previous=nullptr;
}
这是push类应该做的:
>void push(int value)
>
>>Puts the value in a box
>
>>Puts that box on the top of the stack
>
>>Increase m_size by 1
然而,我不认为这是工作,因为我有内存泄漏
泄漏|明确丢失|==| 1个块中的160(16个直接,144个间接)字节在丢失记录2(共2个)中明确丢失|
有人能告诉我我做错了什么吗 您是否清除过堆栈(删除其中的所有内容)?@rubito m_top是如何初始化的?它本身看起来很正常(除了不使用
值
)。使用后,您可能不会正确删除此框。我们需要完整的代码才能更好地提供帮助。@Nik在stackOfBox.h
`private Box*m_top;'@zch我有5个不同的文件?我应该把它们全部加起来吗?
>void push(int value)
>
>>Puts the value in a box
>
>>Puts that box on the top of the stack
>
>>Increase m_size by 1