贝尔福特ufc:谁帮我看看这个程序有没有什么问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 23:03:29
谁帮我看看这个程序有没有什么问题,为什么我一运行总是说"该程序遇到问题需要关闭,......."就像一般系统错误XP的提示一样,不知道这是什么什么.还有我发现只要我一用到指针大不部分的时候都会出现这样的问题.
哪位指点指点.多谢.
#include<iostream>

template<class T,int Size=5>
class stack
{
private:
T s[Size];
int top;
public:
stack()
{
top=0;
}
void push(const T& n);
T pop();
};

template<class T,int Size>
void stack<T,Size>::push(const T& n)//定义stack类的push函数,入栈
{
if(top<Size)
{
s[top]=n;
top++;
}
else
{
std::cout<<"栈满\n";

}
}

template<class T,int Size>
T stack<T,Size>::pop() //定义stack类的pop函数,出栈
{
if(top>0)
{
return s[--top];
}
else
{
std::cout<<"栈空\n";
return s[top];
}
}

class string //定义字符串
{

public:
char str[10];
string() //当无指定字符串时的初始化
{
int i;
for(i=0;i<10;i++)
str[i]='\0';
}
string( char *s)//带参数的构造函数对字符串初始化
{
int i=0;
while(*s!='\0')
{
str[i]=*s;
i++;
s++;
}
str[i]='0';
}

const string operator =(const string& s)//重载运算符"="
{
int i;
while(s.str[i]!='0')
{
str[i]=s.str[i];
i++;
}
str[i]='\0';
return *this;
}
};

int main()
{
int i,n;
stack<string> st;
stack<int ,10> num;
string sc;
for(i=0;i<6;i++) //读字符串入站
{
std::cin>>sc.str;
st.push(sc);
}
for(i=0;i<6;i++)
{
sc=st.pop(); //对st进行出站
std::cout<<sc.str;
}
std::cout<<"\n";

for(i=0;i<10;i++)
{
num.push(i);
std::cout<<i<<" ";
}
std::cout<<"\n";

for(i=0;i<10;i++)
{
n=num.pop();
std::cout<<n<<" ";
}
return 0;
}

1、有两个'\0'你写成了'0'。
2、const string operator =(const string& s)//重载运算符"="
这个函数开始的i你没有初始化为0。

我懒得看了,但是基本可以说明你使用指针的时候有问题,操作了不安全的内存。