首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于包类的STL模板编译异常,处女贴,希望大家帮小弟我看看

2012-11-09 
关于包类的STL模板编译错误,处女贴,希望大家帮我看看编写一个包类的STL模板,但是总是编译失败显示:f:\data

关于包类的STL模板编译错误,处女贴,希望大家帮我看看
编写一个包类的STL模板,但是总是编译失败
显示:f:\data structure\第六章 stl\链表stl类\node.h(66): error C2248: “Node_huteng::Node<Item>::data”: 无法访问 private 成员(在“Node_huteng::Node<Item>”类中声明)

//node.h[code=C/C++][/code]

C/C++ code
#ifndef NODE_H#define NODE_H#include <cstdlib>#include <cassert>#include <iterator>using namespace std;namespace Node_huteng{    template <class Item>    class Node    {        typedef Item value_type;        Node(const Item& init_data = Item(),Node* init_link = NULL)        {            data_field = init_data;            link_field = init_link;        }        Item& data(){return data_field;}        const Item& data() const {return data_field;}        Node* link(){return link_field;}        const Node* link() const{return link_field;}        void set_data(const Item& new_data){data_field = new_data;}        void set_link(Node* new_link){link_field = new_link;}    private:        Item data_field;        Node* link_field;    };    template <class Item>    void list_clear(Node<Item>*& head_ptr);        template <class Item>    void list_head_insert(Node<Item>*& head_ptr,const Item& entry);    template <class Item>    void list_copy(const Node<Item>* source_ptr,Node<Item>*& head_ptr,Node<Item>*& tail_ptr);    template <class Item>    void list_head_remove(Node<Item>*& head_ptr);    template <class Item>    void list_insert(Node<Item>* previous_ptr,const Item& entry);    template <class Item>    size_t list_length(const Node<Item>* head_ptr);    template <class Item>    void list_remove(Node<Item>* previous_ptr);    template <class NodePtr,class SizeType>    NodePtr list_locate(NodePtr head_ptr,SizeType position);    template <class NodePtr,class Item>    NodePtr list_search(NodePtr head_ptr,const Item& target);    template <class Item>    class Node_iterator :public iterator<forward_iterator_tag,Item>        //继承iterator中的前置迭代器标志,数据项为Item    {    public:        Node_iterator(Node<Item>* initial = NULL){current = initial;}        Item& operator * () const {return current->data();}        //可修改数据项里面的数据         Node_iterator& operator ++()            //prefix        {            current = current->link();            return *this;        }        Node_iterator operator ++(int)            //postfix        {            Node_iterator original(current);            current = current->link();            return original;        }        bool operator ==(const Node_iterator other) const        {return current == other.current;}        bool operator !=(const Node_iterator other) const        {return current != other.current;}    private:        Node<Item>* current;    };    template <class Item>    class const_Node_iterator :public iterator<forward_iterator_tag,Item>        //继承iterator中的前置迭代器标志,数据项为Item    {    public:        const_Node_iterator(const Node<Item>* initial = NULL){current = initial;}        const Item& operator *() const{return current->data();}        //可修改数据项里面的数据         const_Node_iterator& operator ++()            //prefix        {            current = current->link();            return *this;        }        const_Node_iterator operator ++(int)            //postfix        {            Node_iterator original(current);            current = current->link();            return original;        }        bool operator ==(const const_Node_iterator other) const        {return current == other.current;}        bool operator !=(const const_Node_iterator other) const        {return current != other.current;}    private:        const Node<Item>* current;    };}#endifnamespace Node_huteng{    template <class Item>    void list_clear(Node<Item>*& head_ptr)    {        while(head_ptr != NULL)            list_head_remove(head_ptr);    }    template <class Item>    void list_copy(const Node<Item>* source_ptr,Node<Item>*& head_ptr,Node<Item>*& tail_ptr)    {        head_ptr = NULL;        tail_ptr = NULL;        if(source_ptr == NULL)            return ;        list_head_insert(head_ptr,source_ptr->data());        tail_ptr = head_ptr;        source_ptr = source_ptr->link();        while(source!=NULL)        {            list_insert(tail_ptr,source_ptr->data());            tail_ptr = tail_ptr->link();            source_ptr = source_ptr->link();        }    }    template <class Item>    void list_head_insert(Node<Item>*& head_ptr,const Item& entry)    {        head_ptr = new Node<Item>(entry,head_ptr);    }    template <class Item>    void list_head_remove(Node<Item>*& head_ptr)    {        Node<Item> *remove_ptr;        remove_ptr = head_ptr;        head_ptr = head_ptr->link();        delete remove_ptr;    }    template <class Item>    void list_insert(Node<Item>* previous_ptr,const Item& entry)    {        Node<Item> *insert_ptr;        insert_ptr = new Node<Item>(entry,previous_ptr->link());        previous_ptr->set_link(insert_ptr);    }    template <class Item>    size_t list_length(const Node<Item>* head_ptr)    {        const Node<Item> *cursor;        size_t answer;                answer = 0;        for(cursor = head_ptr;cursor!=NULL;cursor = cursor->link())            ++answer;        return answer;    }    template <class NodePtr,class SizeType>    NodePtr list_locate(NodePtr head_ptr,SizeType position)    {        NodePtr cursor;        size_t i;        assert(0<position);        cursor = head_ptr;        for(i=1;(i<position) && (cursor != NULL);++i)            cursor = cursor->link();        return cursor;    }    template <class Item>    void list_remove(Node<Item>* previous_ptr)    {        Node<Item> *remove_ptr;        remove_ptr = previous_ptr->link();        previous_ptr->set_link(remove_ptr->link());        delete remove_ptr;    }    template <class NodePtr,class Item>    NodePtr list_search(NodePtr head_ptr,const Item& target)    {        NodePtr cursor;        for(cursor=head_ptr;cursor!=NULL;cursor = cursor->link())            if(target == cursor->data())                return cursor;        return NULL;    }} 




//bag.h[code=C/C++][/code]
C/C++ code
#ifndef BAG_H#define BAG_H#include <cstdlib>#include <cassert>#include "node.h"using namespace Node_huteng;namespace Bag_huteng{    template <class Item>    class Bag    {    public:        typedef std::size_t size_type;        typedef Item value_type;        typedef Node_iterator<Item> iterator;        typedef const_Node_iterator<Item> const_iterator;        Bag();        Bag(const Bag& source);        ~Bag();        size_type erase(const Item& target);        bool erase_one(const Item& target);        void insert(const Item& entry);        void operator +=(const Bag& addend);        void operator =(const Bag& source);        size_type count(const Item& target)const;        Item grab() const;        size_type size() const{return many_nodes;}        iterator begin(){return iterator(head_ptr);}        const_iterator begin() const {return const_iterator(head_ptr);}        iterator end(){return iterator();}        const_iterator end() const{return const_iterator();}    private:        Node<Item> *head_ptr;        size_type many_nodes;    };    template <class Item>    Bag<Item> operator +(const Bag<Item>& b1,const Bag<Item>& b2);}#endifnamespace Bag_huteng{    template <class Item>    Bag<Item>::Bag()    {        head_ptr = NULL;        many_nodes = 0;    }    template <class Item>    Bag<Item>::Bag(const Bag<Item>& source)    {        Node<Item> *tail_ptr;        list_copy(source.head_ptr,head_ptr,tail_ptr);        many_nodes = source.many_nodes;    }    template <class Item>    Bag<Item>::~Bag()    {        list_clear(head_ptr);        many_nodes = 0;    }    template <class Item>    typename Bag<Item>::size_type Bag<Item>::count(const Item& target) const    {        size_type answer;        const Node<Item> *cursor;        answer = 0;        cursor = list_search(head_ptr,target);        while(cursor!=NULL)        {            ++answer;            cursor = cursor->link();            cursor =list_search(cursor,target);        }        return answer;    }    template <class Item>    typename Bag<Item>::size_type Bag<Item>::erase(const Item& target)    {        size_type answer = 0;        Node<Item> *target_ptr;        target_ptr = list_search(head_ptr,target);        while(target_ptr!=NULL)        {            ++answer;            target_ptr->set_data(head_ptr->data());            target_ptr = target_ptr->link();            target_ptr = list_search(target_ptr,target);            list_head_remove(head_ptr);        }        return answer;    }    template <class Item>    bool Bag<Item>::erase_one(const Item& target)    {        Node<Item> *target_ptr;        target_ptr = list_search(head_ptr,target);        if(target_ptr == NULL)            return false;        target_ptr->set_data(head_ptr->data());        list_head_remove(head_ptr);        --many_nodes;        return true;    }    template <class Item>    Item Bag<Item>::grab() const    {        size_type i;        const Node<Item> *cursor;        assert(size() > 0);        i = (std::rand() % size() ) + 1;        cursor = list_locate(head_ptr,i);        return cursor->data();    }    template <class Item>    void Bag<Item>::insert(const Item& entry)    {        list_head_insert(head_ptr,entry);        ++many_nodes;    }    template <class Item>    void Bag<Item>::operator +=(const Bag<Item>& addend)    {        Node<Item> *copy_head_ptr;        Node<Item> *copy_tail_ptr;        if(addend.many_nodes > 0)        {            list_copy(addend.head_ptr,copy_head_ptr,copy_tail_ptr);            copy_tail_ptr->set_link(head_ptr);            head_ptr = copy_head_ptr;            many_nodes += addend.many_nodes;        }    }    template <class Item>    void Bag<Item>::operator =(const Bag<Item>& source)    {        Node<Item> *tail_ptr;        if(this == &source)            return ;        list_clear(head_ptr);        many_nodes = 0;        list_copy(source.head_ptr,head_ptr,tail_ptr);        many_nodes += source.many_nodes;    }    template <class Item>    Bag<Item> operator +(const Bag<Item>& b1,const Bag<Item>& b2)    {        Bag<Item> answer;        answer+=b1;        answer+=b2;        return answer;    }} 


//main[code=C/C++][/code]
C/C++ code
#include <iostream>#include <cstdlib>#include "bag.h"using namespace std;//using namespace Node_huteng;using namespace Bag_huteng;int main(){    Bag<int> test;    test.insert(1);    test.insert(3);    test.insert(5);    test.insert(7);    test.insert(8);    Bag<int>::iterator p;    p = test.begin();    while(p!=test.end())    {        cout << *p << endl;        ++p;    }        return EXIT_SUCCESS;}


[解决办法]
需要public访问权限。
C/C++ code
   template <class Item>    class Node    {      //默认为private      bublic:        typedef Item value_type; 

热点排行