| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 397 人关注过本帖
标题:关于栈的问题
只看楼主 加入收藏
我很无赖
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-5-20
收藏
 问题点数:0 回复次数:1 
关于栈的问题

今天小弟刚学栈,编完后有几个错误该不了
//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编辑过]

搜索更多相关主题的帖子: private include public 
2007-06-01 18:57
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
template<class T>T stack<T>::top()
{
if(tos>0)
return list[tos-1];
}
这里如果tos<=0时,就没有返回值。所以可以去掉if(tos>0)这一行。
如果你非要检查,那就把这一行换成assert(tos>0);就可以了

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-01 21:05
快速回复:关于栈的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022427 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved