C++ 堆栈列表调试断言失败c++;

C++ 堆栈列表调试断言失败c++;,c++,list,stack,C++,List,Stack,当我尝试运行代码时,它显示调试断言失败。任何人都可以帮助我,我正在执行堆栈列表,在头文件中,我创建了一个结构,它有三个变量,string*s,int numoflength,stackFrame*next void Stack::push(string& s) { StackFramePtr temp_ptr; temp_ptr=new StackFrame; temp_ptr->s

当我尝试运行代码时,它显示调试断言失败。任何人都可以帮助我,我正在执行堆栈列表,在头文件中,我创建了一个结构,它有三个变量,string*s,int numoflength,stackFrame*next

     void Stack::push(string& s)
        {

            StackFramePtr temp_ptr;
            temp_ptr=new StackFrame;
            temp_ptr->str=new string[s.size()];
            (temp_ptr->str)[0]=s;
            cout<<temp_ptr->str[0]<<endl;
            temp_ptr->num_char=sizeofstring(s);
            if(empty())
            {
                top=temp_ptr;
                temp_ptr->next=NULL;
            }
            else
            {
                temp_ptr->next=top;
                top=temp_ptr;
            }
        }

this is my code about push I think maybe those errors because of this function.
string Stack::pop()
{
    if(empty())
        exit(1);
    string * name;
    StackFramePtr temp;
    temp=top;
    name=top->str;
    top=top->next;
    delete temp;
    return *name;
}

#include <iostream>
#include "stack.h"
#include <string>
using namespace std;
int main()
{
    string str1="to";
    string str2="hi";
    string str3="food";
    string str4="ba";
    string str5="ti";
    string str6="zhilong";
    Stack s;
    s.push(str1);
    s.push(str2);
    s.push(str3);
    s.push(str4);
    s.push(str5);
    s.push(str6);
    cout<<s;
    system("pause");
    return 0;
}
void Stack::push(字符串&s)
{
堆垛机机架温度;
temp_ptr=新堆叠框架;
temp_ptr->str=新字符串[s.size()];
(temp_ptr->str)[0]=s;
coutnext=top;
top=温度ptr;
}
}
这是我关于推送的代码,我想这些错误可能是因为这个函数。
字符串堆栈::pop()
{
if(空())
出口(1);
字符串*名称;
堆垛机温度;
温度=顶部;
name=top->str;
顶部=顶部->下一步;
删除临时文件;
返回*名称;
}
#包括
#包括“stack.h”
#包括
使用名称空间std;
int main()
{
字符串str1=“to”;
字符串str2=“hi”;
string str3=“食品”;
字符串str4=“ba”;
字符串str5=“ti”;
字符串str6=“志龙”;
堆栈s;
s、 push(str1);
s、 推送(str2);
s、 推送(str3);
s、 推送(str4);
s、 push(str5);
s、 推送(str6);

cout在
push
中分配一个
string
数组,并将其分配给
Stack
str
成员。在
pop
中,将
str
复制到
name
,然后
删除temp
(我假设)将删除名称现在指向的数组。最后,取消引用此悬空指针并访问已释放的内存


要解决此问题,请将
name
声明为一个
string
,而不是字符串指针,然后设置
name=*top->str
name=top->str[0]

是否创建了堆栈类?是的。我创建了一个堆栈类,而pop和push都是公共成员函数。是否可以显示堆栈类?类堆栈{public:Stack();~Stack();void push(string&str);string pop();StackFramePtr remove_strings_length(int length);bool empty();friend ostream&operator(istream&in_stream,Stack&mystack);private:StackFramePtr top;};我使用name=top->str[0],然后删除temp,但它仍然给我调试断言失败,它说字符串下标超出范围。
friend ostream&Operator的代码您可以检查我的ostream,但我觉得在ostream中没有错误,可能是因为push,因为在主函数中,我只使用默认和push函数以及cout。它是否告诉您debu的位置g断言失败?如果你在调试器中运行它,你可以查找调用堆栈,看看代码的哪个部分有错误。不,它没有说失败在哪里,我不太确定如何使用调试器,你能告诉我如何使用它吗?
class Stack
{
public:
    Stack();
    //Default Constructor used to create an empty Stack object.

    ~Stack();
    //Destructor for Stack objects.

    void push(string& str);

    string pop();

    bool empty();
    //Checks to see if the Stack is empty.  Returns true if empty, else returns false.
    //Stack remains unchanged after function call.


    friend ostream &operator<<(ostream & out_stream, Stack & mystack);

    friend istream &operator>>(istream & in_stream, Stack & mystack);

private:
    StackFramePtr top; // Points to the top of the stack;

};
ostream &operator<<(ostream & outs, Stack & sta)
{
    if(sta.empty())
        exit(1);
    else
    {
        StackFramePtr read;
        read=sta.top;
        while(read!=NULL)
        {
            outs<<"string = "<<read->str[0]<<endl;
            outs<<" number of charcter is" <<read->num_char;
            read=read->next;
            outs<<endl<<endl;
        }
    }
    return outs;
}