| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1021 人关注过本帖
标题:按书建栈,编译没通过
只看楼主 加入收藏
dubaoshi
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2006-9-22
收藏
 问题点数:0 回复次数:11 
按书建栈,编译没通过
我按书上输了一个栈实例,但通不过编译,请教高手:
#include<iostream>
#include<string>
#include<vector>

using namespace std;

class istack{
      public:
      iStack( int capacity )//这里就通不过了
: _stack( capacity ), _top( 0 ) {}

      bool pop(int &value);
      bool push(int value);

      bool full();
      bool empty();
      void display();

      int size();
      private:
      int _top;
      vector<int>_stack;
      };

      inline bool istack::size(){return _top;};
      inline bool istack::empty()
      {return _top?false:true;}

      inline bool istack:full()
      {return _top<_stack.size()-1?false:true;}

      bool istack::pop(int &top_value)
      {
           if(empty())
           return false;

           top_value=stack[--_top];
           cout<<"istack:pop():"<<top_value<<endl;

           return true;
      }

      bool istack::push(value)
      {
           cout<<"istack::push()("<<value<<")\n";
           if(full())
           return false;

           _stack[_top++]=value;
           return true;
      }

      void istack::display()
      {
           if(!size())
           {cout<<"(0)\n";return;}
           cout<<"("<<size()<<")(bot:";
           for(int ix=0;ix<_top;++ix)
           cout<<_stack[ix]<<" ";
           cout<<":top)\n";
      }

      int main()
      {
          istack stack(32);
          stack.display();
          for(int ix=1;ix<51;++ix)
          {
                  if(ix%2==0)stack.push(ix);
                  if(ix%5==0)stack.display();
                  if(ix%10==0){
                               int dummy;
                               stack.pop(dummy);stack.pop(dummy);
                               stack.display();
                               }
          }
      }
搜索更多相关主题的帖子: 编译 
2008-05-08 16:42
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
stack[--_top];
??这个哪来的

学习需要安静。。海盗要重新来过。。
2008-05-08 18:45
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class istack
{
      public:
      istack( int cap ): _capacity( cap ), _top(0) {}

      bool pop();
      bool push(int value);

      bool full();
      bool empty();
      void display();

      int size();
      private:
          vector<int>::size_type _top;
          int _capacity;
          vector<int> _stack;
      };

      inline int istack::size(){return _capacity;};
      inline bool istack::empty()
      {return _top?false:true;}

      inline bool istack::full()
      {return _top<size()-1?false:true;}

      bool istack::pop()
      {
           if(empty())
           return false;
           int i=_stack.at(_top);
           _stack.pop_back();
           cout<<"istack:pop():"<<i<<endl;
           _top--;
           return true;
      }

      bool istack::push(int value)
      {
           
           if(full())
           return false;
           _top++;
           _stack.push_back(value);
           cout<<"istack::push()("<<value<<")\n";
           return true;
      }

      void istack::display()
      {
           if(!size())
           {cout<<"(0)\n";return;}
           cout<<"("<<size()<<")(bot:";
           for(vector<int>::size_type ix=_top-1;ix>0;ix--)
           cout<<_stack.at(ix)<<" ";
           cout<<":top)\n";
      }

      int main()
      {
          istack stack(32);
          for(int ix=0;ix<stack.size();++ix)
          {
                  stack.push(ix);
           }
          stack.display();
          return 0;
      }

学习需要安静。。海盗要重新来过。。
2008-05-08 19:37
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
栈模板是适配器...一般是用向量和队列做基础构建..

学习需要安静。。海盗要重新来过。。
2008-05-08 20:24
dubaoshi
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2006-9-22
收藏
得分:0 
回答楼上的朋友,这个是c++ primer 3th中文版中的一道题。

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-05-09 15:44
dubaoshi
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2006-9-22
收藏
得分:0 
再请教下,楼上的朋友写的是没有错的,但里面有一句,我不知道为什么要这样写(因为根本没定义过_stack.at(_top),还有为什么dev c++可以通过编译,而C-FREE不能通过编译。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class istack
{
      public:
      istack( int cap ): _capacity( cap ), _top(0) {}

      bool pop();
      bool push(int value);

      bool full();
      bool empty();
      void display();

      int size();
      private:
          vector<int>::size_type _top;
          int _capacity;
          vector<int> _stack;
      };

      inline int istack::size(){return _capacity;};
      inline bool istack::empty()
      {return _top?false:true;}

      inline bool istack::full()
      {return _top<size()-1?false:true;}

      bool istack::pop()
      {
           if(empty())
           return false;
           int i=_stack.at(_top);//就是这一句,为什么可以通过编译呢?at是什么???
           _stack.pop_back();
           cout<<"istack:pop():"<<i<<endl;
           _top--;
           return true;
      }

      bool istack::push(int value)
      {
           
           if(full())
           return false;
           _top++;
           _stack.push_back(value);
           cout<<"istack::push()("<<value<<")\n";
           return true;
      }

      void istack::display()
      {
           if(!size())
           {cout<<"(0)\n";return;}
           cout<<"("<<size()<<")(bot:";
           for(vector<int>::size_type ix=_top-1;ix>0;ix--)
           cout<<_stack.at(ix)<<" ";
           cout<<":top)\n";
      }

      int main()
      {
          istack stack(32);
          for(int ix=0;ix<stack.size();++ix)
          {
                  stack.push(ix);
           }
          stack.display();
          return 0;
      }


人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-05-09 15:50
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
这个函数是vector的...用法就是把删除的元素显示到屏幕...建议用vc++6.0以上版本

学习需要安静。。海盗要重新来过。。
2008-05-09 16:25
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
有点小问题改正了
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class istack
{
      public:
      istack( int cap ): _capacity( cap ), _top(0) {}

      bool pop();
      bool push(int value);

      bool full();
      bool empty();
      void display();

      int size();
      private:
          vector<int>::size_type _top;
          int _capacity;
          vector<int> _stack;
      };

      inline int istack::size(){return _capacity;};
      inline bool istack::empty()
      {return _top?false:true;}

      inline bool istack::full()
      {return _top<size()-1?false:true;}

      bool istack::pop()
      {
           if(empty())
           return false;
           int i=_stack.at(_top-1);
           _stack.pop_back();
           cout<<"istack:pop():"<<i<<endl;
           _top--;
           return true;
      }

      bool istack::push(int value)
      {
           
           if(full())
           return false;
           _top++;
           _stack.push_back(value);
           cout<<"istack::push()("<<value<<")\n";
           return true;
      }

      void istack::display()
      {
           if(!size())
           {cout<<"(0)\n";return;}
           cout<<"("<<size()<<")(bot:";
           for(vector<int>::size_type ix=_top-1;ix>0;ix--)
           cout<<_stack.at(ix)<<" ";
           cout<<":top)\n";
      }

      int main()
      {
          istack stack(32);
          for(int ix=0;ix<stack.size();++ix)
          {
                  stack.push(ix);
           }
          stack.display();
          stack.pop();
          stack.pop();
          return 0;
      }

学习需要安静。。海盗要重新来过。。
2008-05-09 16:28
dubaoshi
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2006-9-22
收藏
得分:0 
再次请教楼上的朋友
生成的可执行程序有点大(一般都500~600K),怎么能叫他变小一些啊?我感觉这样的程序的话,几十K够了呢。

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-05-10 08:17
dubaoshi
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2006-9-22
收藏
得分:0 
最初的程序已修改至正确了,修改的地方如下所示:

#include<iostream>
#include<string>
#include<vector>

using namespace std;

class istack{
      public:
      iStack( int capacity )//这里应该是istack(S的大写不对了)
: _stack( capacity ), _top( 0 ) {}

      bool pop(int &value);
      bool push(int value);

      bool full();
      bool empty();
      void display();

      int size();
      private:
      int _top;
      vector<int>_stack;
      };

      inline bool istack::size(){return _top;}//这里的bool应为int
      inline bool istack::empty()
      {return _top?false:true;}

      inline bool istack:full()//这里的:就为::
      {return _top<_stack.size()-1?false:true;}

      bool istack::pop(int &top_value)
      {
           if(empty())
           return false;

           top_value=stack[--_top];//这里的stack应为_stack
           cout<<"istack:pop():"<<top_value<<endl;

           return true;
      }

      bool istack::push(value)//这里的value应为int value
      {
           cout<<"istack::push()("<<value<<")\n";
           if(full())
           return false;

           _stack[_top++]=value;
           return true;
      }

      void istack::display()
      {
           if(!size())
           {cout<<"(0)\n";return;}
           cout<<"("<<size()<<")(bot:";
           for(int ix=0;ix<_top;++ix)
           cout<<_stack[ix]<<" ";
           cout<<":top)\n";
      }

      int main()
      {
          istack stack(32);
          stack.display();
          for(int ix=1;ix<51;++ix)
          {
                  if(ix%2==0)stack.push(ix);
                  if(ix%5==0)stack.display();
                  if(ix%10==0){
                               int dummy;
                               stack.pop(dummy);stack.pop(dummy);
                               stack.display();
                               }
          }
         //这里应加入system("pause");来暂停一下程序显示(dev c++中不加就一闪而过了)
         //这里还应加个return 0,不加也没问题,但是应该养成好习惯的吧:)

      }



总结一下:看着书打都能犯下这么多的错误,怪自己太粗心大意了。编译没通过也没好好查看一下,怪自己态度不大对啊。虽然说程序现在能运行了,但叫我自己写的话,1天也不见得写得出来,再加油学习吧~
尽是一些问题,导致程序不能运行。。。不知道高手是不是也经常犯这样的问题呢?

挺喜欢这小兔子,哈哈~~~


[[it] 本帖最后由 dubaoshi 于 2008-5-10 09:34 编辑 [/it]]

人行善,福虽未至,祸已远离;人行恶,祸虽未至,福已远离.
2008-05-10 09:30
快速回复:按书建栈,编译没通过
数据加载中...
 
   



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

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