今天小弟刚学栈,编完后有几个错误该不了
//stack.h
#ifndef STACK_H
#define STACK_H
#include<iostream>
using namespace std;
const int size=100;
template<class T>class stack
{
public:
stack();
void push(const T&item);//栈添加新数据
T top();//返回栈顶的数据
void pop();//将栈顶数据弹出
private:
T list[size];
int tos;
};
template<class T>stack<T>::stack()
{
tos=0;
}
template<class T>void stack<T>::push(const T&item)
{
if(tos<size)
{
list[tos]=item;
tos++;
}
else
cout<<"cannot add to a full stack"<<endl;
}
template<class T>void stack<T>::pop()
{
if(tos>0)
tos--;
else
cout<<" cannot remove from an empty stack"<<endl;
}
template<class T>T stack<T>::top()
{
if(tos>0)
return list[tos-1];
}
#endif
//main.cpp
#include<iostream>
#include<string>
#include"stack.h"
using namespace std;
void main()
{
stack<double>d_stack;
stack<string>s_stack;
double a[10];
cout<<"请输入范围为100.0到200.0的10个浮点数"<<endl;
for(int i=0;i<10;i++)
{
cin>>a[i];
d_stack.push(a[i]);
}
for(int i=0;i<3;i++)
d_stack.pop();
for(int i=0;i<7;i++)
cout<<d_stack.top();
string b[5];
cout<<"请输入5个朋友的名字"<<endl;
for(int i=0;i<5;i++)
{
cin>>b[i];
s_stack.push(b[i]);
}
for(int i=0;i<5;i++)
{
cout<<s_stack.top();
s_stack.pop();
}
}
Compiling...
main.cpp
f:\dsa\stack.h(43) : warning C4715: 'stack<double>::top' : not all control paths return a value
f:\dsa\stack.h(43) : warning C4715: 'stack<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >::top' : not all control paths return a value
Linking...
dsa.exe - 0 error(s), 2 warning(s)
[此贴子已经被作者于2007-6-1 19:21:18编辑过]