标题:编译通不过了,请教
只看楼主
dubaoshi
Rank: 1
等 级:新手上路
帖 子:118
专家分:0
注 册:2006-9-22
 问题点数:0 回复次数:0 
编译通不过了,请教
这个是我按书上抄的一个程序(C++ primer 3th中文),我也知道第一次编译不会通过的(对自己没自信),但错在哪里,我实在有些看不懂。
这个程序是个练习链表的,包括2个类,一个类里包括数据和指针,另一个类包括实现各种操作(插入、删除等)
请教,我要如何修改?


#include<iostream>
class ilist_item;
class ilist{
public:
    //缺省构造函数
    ilist():_at_front(0),_at_end(0),_size(0){}
    int size();
    void bump_up_size();
    void bump_down_size();
    void insert(ilist_item* ptr,int value);
    void insert_front(int value);
    void insert_end(int value);
    ilist_item* find(int value);
    void display(ostream &os=cout);//`ostream' `os'`cout'was not declared in this scope
private:
    ilist_item* _at_front;
    ilist_item* _at_end;
    
    int _size;
    
    ilist(const ilist&);
    ilist& operator=(const ilist&);
    
};
inline int ilist::size(){return _size;}
inline void ilist::bump_up_size(){++_size;}
inline void ilist::bump_down_size(){--_size;}
inline void ilist::insert(ilist_item* ptr,int value)
{
    if(!ptr)
        insert_front(value);
    else{
        bump_up_size();
        new ilist_item(value,ptr);
    }
}
inline void ilist::insert_front(int value)
{
    ilist_item *ptr=new ilist_item(value);
    if(!_at_front)
        _at_front=_at_end=ptr;
    else{
        ptr-next(_at_front);
        _at_front=ptr;
    }
    bump_up_size();
}

inline void ilist::insert_end(int value)
{
    if(!_at_end)
        _at_end=_at_front=new ilist_item(value);
    else
        _at_end=new ilist_item(value,_at_end);
    bump_up_size();
}

ilist_item* ilist::find(int value)
{
    ilist_item *ptr=_at_front;
    while(ptr)
    {
        if(ptr->value()==value)
            break;
        ptr=ptr-next();
    }
    return ptr;
}
void display(ostream &os)
{
    os<<"\n("<<_size<<")(";
    ilist_item *ptr=_at_front;
    
    while(ptr){
        os<<ptr->value()<<" ";
        ptr=ptr->next();
    }
    os<<")\n";
}

class ilist_item{
public:
    ilist_item(int value,ilist_item* item_to_link_to=0);
    int value(){return _value;}
    ilist_item *next(){return _next;}
    void next(ilist_item *link){_next=link;}
    void value(int new_value){_value=new_value;}
    
private:
    int _value;
    ilist_item* _next;
};
inline ilist_item::ilist_item(int value,ilist_item* item)
:_value(value)
{
    if(!item)
        _next=0;
    else{
        _next=item->_next;
        item->_next=this;
    }
}

int main()
{
    ilist mylist;
    
    for(int ix=0;ix<10;++ix){
        mylist.insert_front(ix);
        mylist.insert_end(ix);
    }
    
    cout<<"OK:after insert_front()and insert)_end()\n";
    
    mylist.display();
    
    ilist_item *it=mylist.find(8);
    cout<<"\n"<<"Searching for the value 8:found it<<"
    <<(it<<"yes!\n":"no!\n");
    mylist.insert(it,1024);
    
    cout<<"\n"<<"Inserting element 1024 following the value 8\n";
    mylist.display();
    
    int elem_cnt=mylist.remove(8);
    cout<<"\n"<<"Removed"<<elem_cnt<<" of the value 8\n";
    mylist.display();
    
    cout<<"\n"<<"Removed front element\n";
    
    mylist.remove_front();
    mylist.display();
    
    cout<<"\n"<<"Removed all elements\n";
    mylist.remove_all();
    mylist.display();
}

程序有点长~~~
搜索更多相关主题的帖子: void ilist 编译 bump 
2008-05-13 11:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-214055-1-1.html




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

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