【发布时间】:2016-01-08 02:17:13
【问题描述】:
当我尝试运行我的代码时,它显示调试断言失败。任何人都可以帮助我,我正在做堆栈列表,在头文件中,我创建了一个具有三个变量的结构,字符串 * s,int numoflength, stackFrame * 下一个
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;
}
当我尝试运行这个主函数时,它给我调试失败,任何人都可以帮助我吗?非常感谢
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;
}
【问题讨论】:
-
你创建了 Stack 类吗?
-
是的。我确实创建了一个堆栈类,并且 pop 和 push 都是公共成员函数
-
你能显示堆栈类吗?
-
类堆栈 { public: Stack(); 〜堆栈();无效推送(字符串& str);字符串弹出(); StackFramePtr remove_strings_length(int length);布尔空();朋友 ostream &operator>(istream & in_stream, Stack & mystack);私有的:StackFramePtr 顶部; };