关于C++程序 跪求高手

来源:百度知道 编辑:UC知道 时间:2024/06/01 11:27:13
这里有个程序 希望看看哪里错误#include "iostream.h"
template<class T,int size=5>
class stack
{
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)
{
if(top<size)
{
s[top]=n;
top++;
}
else
cout<<"栈满!007\n";
template<class T,int size> T stack<T,size>::pop()
{
if(top>0)
{return s[--top];
}
else
{
cout<<"栈满!007\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++;

问题:
1.有些函数的大括号没有加对。
2.string是c++库中早已经定义好的,你再定义string类,在使用的时候程序不知道到底是使用你定义的还是使用类库定义的。我修改成了sstring
3.栈空的时候还是输出“栈空”好吧?
4.修改了一下include.

下面是我修改后的代码,你看看吧

#include<iostream>
using namespace std;

template<class T,int size=5>
class stack
{
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)
{
if(top<size)
{
s[top]=n;
top++;
}
else
cout<<"栈满!007\n";
}

template<class T,int size> T stack<T,size>::pop()
{
if(top>0)
{
return s[--top];
}
else
{
cout<<"栈空!007\n";
return s[top];
}
}

class sstring
{