C ++模板对象组合 - 如何正确初始化

时间:2015-03-21 16:17:41

标签: c++ list templates tree composition

我发布此信息是因为我在创建新数据结构时遇到了小问题。

它可能看起来很奇怪,但我想创建一个树列表。

我用这样的构造函数创建了我的Tree类:

template <class T> Tree<T>::Tree(Tree<T> *MyFather=NULL);

此构造函数初始化树变量并在其私有区域中设置指向其父亲的指针。

现在:我有我的列表,使用这个构造函数:

template <class T> List<T>::List(int NElems,T Value);

使用T值创建NElems元素。

我的问题是,如何在主函数中创建树列表?

如果我使用这种指令创建它:

List<Tree<int> >Lista1(0,0);

编译器给了我这些错误:

给出的错误是:

[Error] no matching function for call to 'Tree<int>::Tree(Tree<int>)'

我该如何解决这个问题?你能救我吗?

这是我的模板列表类:

template <class T> class List{

    public:

        friend ostream &operator<< (ostream &Out,List<T> &ListToPrint){

            int Counter;

            for (Counter=0;Counter<ListToPrint.GetNElems();Counter++){


                Out << "Indirizzo #" << setw(2) << Counter+1 << ": " << &ListToPrint[Counter] << " - Valore: " << setw(2) << ListToPrint[Counter];
                if (Counter!=ListToPrint.GetNElems()-1) Out << endl;
            }

            Out << "\n\nLa Lista e': " << (ListToPrint.IsEmpty()==true ? "vuota" : "non vuota") << endl
                 << "Totale Elementi: " << ListToPrint.GetNElems() << endl 
                 << "Primo Elemento: " << ListToPrint.FirstElement << endl 
                 << "Ultimo Elemento: " <<ListToPrint.LastElement << endl 
                 << "--------------------" <<endl;

            return Out;
        }

        friend class Cache<T>;
        T &operator[](int Index);

        stUtility operator()(int Index);

        List <T> &operator=(ListNode<T> *ListNode2Copy){

            ListNode<T> *Element=ListNode2Copy;

            while (Element!=NULL){

                AddElement(LIST_END,1);
                *SelectElement(LIST_END)=Element->GetValue();
                Element=Element->GetNextElement();  
            }

            return *this;
        }


        List <T> &operator=(List<T> &List2Copy){

            if (&List2Copy!=this){

                if (NElems<List2Copy.GetNElems()){

                    AddElement(LIST_START,List2Copy.GetNElems()-GetNElems());
                }
                else if (NElems>List2Copy.GetNElems()){

                    int Counter;
                    int ForLength=NElems-List2Copy.GetNElems();

                    for (Counter=0;Counter<ForLength;Counter++){

                        DeleteElement(&(*this)[LIST_START]);
                    }
                }

                int Counter;

                for (Counter=0;Counter<NElems;Counter++){

                    (*this)[Counter]=List2Copy[Counter];    
                }
            }
            else{

                cout << "Auto Assignment, operator=() function call ignored.\n\n";
            }
            return *this;   
        }

        List(int Elems, T Value2Assign);

        List(List<T> &List2Copy){

            NElems=0;
            SetFirstElement(NULL);
            SetLastElement(NULL);

            int Counter;

            for (Counter=0;Counter<List2Copy.GetNElems();Counter++){

                AddElement(LIST_END,1);
                (*this)[Counter]=List2Copy[Counter];
            }
        }


        T &GetFirstElement();
        T &GetLastElement();

        bool IsEmpty();
        bool AddElement(int Position,int N, T Value2Assign);
        bool WriteElement(T Value,int Position);

        bool DeleteElement(ListNode<T> *Elem);

        int GetNElems();
        ~List();
        ListNode<T> &SearchElement(T Value,int Mode);
        void MoveElement(stUtility Dest,stUtility Source);
        void Reverse();
        void Order(int Mode);
        void RemoveDuplicatesOrdered();
        void RemoveDuplicates();
        void Import(int ToPosition, int FromPosition, List<T> &Node2Import, int ImportQuantity);

    private:

        void SetFirstElement(ListNode<T> *Elem){

            FirstElement=Elem;
            if (FirstElement!=NULL)FirstElement->SetPrevElement(NULL);
        }
        void SetLastElement(ListNode<T> *Elem){

            LastElement=Elem;
            if (LastElement!=NULL) LastElement->SetNextElement(NULL);
        }

        ListNode<T> *SelectElement(int Index);

        int NElems;
        ListNode<T> *FirstElement;
        ListNode<T> *LastElement;
        bool Closing;

        Cache<T> ListCache;
};

我的列表由ListNodes组成,它们属于这种类型:

template <class T> class ListNode{

    friend ostream &operator<< (ostream &Out, ListNode<T> &ListToPrint){

        if (&ListToPrint!=NULL) Out << ListToPrint.GetValue();
        else Out << "N/D";
        return Out;
    }

    public:

        ListNode(T Value);
        ~ListNode();

        T &GetValue();

        void SetValue(T Value);

        void SetNextElement(ListNode *NextElem);
        void SetPrevElement(ListNode*PrevElem);

        ListNode <T> *GetPrevElement();

        ListNode <T> *GetNextElement();

        const bool operator==( ListNode<T> &ListNodeToCMP){

            if (Value==ListNodeToCMP.GetValue()) return true;
            else return false;
        }

        const bool operator==( T ValueToCMP){

            if (Value==ValueToCMP) return true;
            else return false;
        }

        const bool operator<( ListNode<T> &ListNodeToCMP){

            if (Value<ListNodeToCMP.GetValue()) return true;
            else return false;
        }

        const bool operator>( ListNode<T> &ListNodeToCMP){

            if (*this<ListNodeToCMP || *this==ListNodeToCMP) return false;
            else return true;
        }

        const bool operator<=( ListNode<T> &ListNodeToCMP){

            if (*this<ListNodeToCMP || *this==ListNodeToCMP) return true;
            else return false;
        }

        const bool operator>=( ListNode<T> &ListNodeToCMP){

            if (*this==ListNodeToCMP || *this>ListNodeToCMP) return true;
            else return false;
        }

        const bool operator!=( ListNode<T> &ListNodeToCMP){

            if (*this==ListNodeToCMP) return false;
            else return true;
        }

          ListNode <T> &operator=(T Value){

            SetValue(Value);
            return *this;
        }

          ListNode <T> &operator=(ListNode<T> &LNode2Copy){

            SetValue(LNode2Copy.GetValue());
            return *this;
        }

    protected:

        ListNode <T>* NextElement;
        ListNode <T>* PrevElement;

        T Value;
};
#include "ListNodeImplementation.h" 

这是我的Tree模板类:

    #include "ListHeader.h"
#define LEFT -3
#define RIGHT -2
#define PREV -6
#define POS_UNDEFINED -8
#define NEXT -7
#define MYSELF -9
#define LAST_USED -10
#define ALL -1
#define BINARY -4
#define UNDEFINED -5
#define INCLUDE_ALL -11
#define ROOT_ONLY -12
#define SONS_ONLY -13

template <class T> class Tree{

    friend ostream &operator<< (ostream &Out, Tree<T> &TreeToPrint){

        if (&TreeToPrint!=NULL){

            int Counter=0;
            int ElemsCounter=1;
            int i=0;
            int Depth=0;
            bool End=false;
            TreeToPrint.SetLastPosUsed(POS_UNDEFINED);
            Tree<T> *Element=&TreeToPrint;

            Out << "Valore Radice: " << TreeToPrint.GetValue() << endl;

            while (End==false && TreeToPrint.GetNElems()>0){

                Depth++;

                for (int c=0;c<Depth;c++){

                    Out <<"     .";
                }   

                Out << endl;

                for (int c=0;c<Depth;c++){


                    Out <<"     .";
                }   

                if (Counter==0){

                    Element->SetLastPosUsed(POS_UNDEFINED);
                    Counter=1;
                }

                Out << " ->> Valore Nodo: " << (*Element)(NEXT).GetValue() << endl;     

                if ((*Element)(LAST_USED).GetNElems()>0){

                    Element=&(*Element)(LAST_USED);
                    Counter=0;
                }
                else{

                    if (((*Element)(LAST_USED).IsLastSon())){

                        for (int c=0;c<Depth-1;c++){

                            Out <<"     .";
                        }   
                        if (Depth!=1){

                            Out << "     +";
                            Out << endl;    
                        }

                        if (Depth-2>0 && Element->IsLastSon()){

                            for (int c=0;c<Depth-2;c++){


                                Out <<"     .";
                            }       
                            Out << "     +";
                            Out << endl;    
                        }

                        bool First=true;

                        do{

                            if (First==true) First=false;
                            else if (First==false && (*Element).IsLastSon() && Depth>1){

                                if (Depth-2>0){

                                    for (int c=0;c<Depth-2;c++){

                                        Out <<"     .";
                                    }   

                                    Out << "     +";
                                    Out << endl;    
                                }

                                if (Depth-3>0 && Element->GetFather().IsLastSon()){

                                    for (int c=0;c<Depth-3;c++){


                                        Out <<"     .";
                                    }       
                                    Out << "     +";
                                    Out << endl;    
                                }
                                First=true;
                            }

                            if (&(*Element).GetFather()!=NULL && &TreeToPrint!=Element){

                                Depth--;
                                Element=&(Element->GetFather());    
                            }

                            Counter=1;
                        }while ((*Element)(LAST_USED).IsLastSon() && &(*Element).GetFather()!=NULL && &(*Element).GetFather()!=&TreeToPrint.GetFather());

                        if (Element==&TreeToPrint && (*Element)(LAST_USED).IsLastSon()){

                            Depth--;
                            End=true;   
                            Out << "     #";
                        }
                    }
                    Depth--;
                }
            }
            Out << endl << endl;

            Out << "Pre-order  View: " << endl;
            TreeToPrint.PreOrderView();

            Out << endl << endl;

            Out << "Post-order View: " << endl;
            TreeToPrint.PostOrderView();

            Out << endl << endl;

            Out << "In-order   View: " << endl;
            TreeToPrint.InOrderView();

            Out << endl << endl;
        }
        else{

            Out << "Nothing to print.\n\n";
        }

        return Out;
    }

    public:

        Tree(Tree<T> *Father=NULL);     

        Tree(Tree<T> &TreeToCopy) : MyFather(NULL){

            *this=TreeToCopy.GetValue();

            SetSonsLimit(TreeToCopy.GetSonsLimit());

            int Counter=0;
            int Depth=0;
            bool End=false;

            Tree<T> *Element=this;
            Tree<T> *Element2=&TreeToCopy;

            while (End==false && Element2->GetNElems()>0){

                Depth++; 

                if (Counter==0){

                    Element->SetLastPosUsed(POS_UNDEFINED);
                    Element2->SetLastPosUsed(POS_UNDEFINED);

                    for (int i=0;i<Element2->GetNElems();i++){

                        Element->NewSon(RIGHT,(*Element2)(NEXT).GetValue());
                    }

                    Element2->SetLastPosUsed(POS_UNDEFINED);
                    Element->SetSonsLimit(Element2->GetSonsLimit());
                    Counter=1;
                }

                (*Element)(NEXT);
                (*Element2)(NEXT);

                if ((*Element2)(LAST_USED).GetNElems()>0){

                    Element=&(*Element)(LAST_USED);
                    Element2=&(*Element2)(LAST_USED);
                    Counter=0;
                }
                else{

                    if (((*Element2)(LAST_USED).IsLastSon())){

                        do{

                            Depth--;
                            if (&(*Element).GetFather()!=NULL && &(*Element2).GetFather()!=NULL&& Element2!=Element && Element2!=&TreeToCopy){

                                Element=&(Element->GetFather());
                                Element2=&(Element2->GetFather());  
                            }
                            Counter=1;
                        }while ((*Element)(LAST_USED).IsLastSon() && &(*Element).GetFather()!=NULL && &(*Element).GetFather()!=&this->GetFather());



                        if ((Element2==&TreeToCopy && ((*Element2)(LAST_USED).IsLastSon()))){

                            End=true;   
                        }
                    }

                    Depth--;
                }
            }   
        }

        ~Tree(){

            delete Nodes;
        }

        Tree<T> &NewSon(int Mode=RIGHT, T Value=(T)0);
        Tree<T> &GetSon(int Position=LEFT);
        void Delete(int Mode, int SonPosition);
        void Import(int SonPosition,Tree<T> Tree2Import,int Mode);
        Tree<T> &SearchNode(T Value2Search);

        void SetValue(T Value);
        void SetHeight(int Value);
        void SetDeepness(int Value);

        T GetValue();
        int GetDeepness();
        int GetHeight();
        int GetNElems();

        static int GetSonsLimit();
        static void SetSonsLimit(int Limit=BINARY);

        Tree<T> &GetFather();
        void SetFather(Tree<T> &Father);

        bool IsLastSon();
        bool IsFirstSon();
        void PreOrderView();
        void InOrderView();
        void PostOrderView();

        void SetLastPosUsed(int Pos){

            LastPosUsed=Pos;
        }

        int GetLastPosUsed(){

            return LastPosUsed;
        }
        bool operator==(Tree<T> &TreeToCMP){

            //SetSonsLimit(TreeToCopy.GetSonsLimit());

            int Counter=0;
            int Depth=0;
            bool End=false;

            Tree<T> *Element=this;
            Tree<T> *Element2=&TreeToCMP;
            if (this->GetValue()!=TreeToCMP.GetValue()) return false;
            else
            while (End==false && Element2->GetNElems()>0 && Element->GetNElems()==Element2->GetNElems()){

                Depth++; 

                if (Counter==0){

                    Element->SetLastPosUsed(POS_UNDEFINED);
                    Element2->SetLastPosUsed(POS_UNDEFINED);

                    for (int i=0;i<Element2->GetNElems();i++){

                        if ((*Element)(NEXT).GetValue()!=(*Element2)(NEXT).GetValue()){

                            return false;
                        }
                    }
                    Element->SetLastPosUsed(POS_UNDEFINED);
                    Element2->SetLastPosUsed(POS_UNDEFINED);

                    Counter=1;
                }

                (*Element)(NEXT);
                (*Element2)(NEXT);

                if ((*Element2)(LAST_USED).GetNElems()>0){

                    Element=&(*Element)(LAST_USED);
                    Element2=&(*Element2)(LAST_USED);
                    Counter=0;
                }
                else{

                    if (((*Element2)(LAST_USED).IsLastSon())){

                        do{

                            Depth--;
                            if (&(*Element).GetFather()!=NULL && &(*Element2).GetFather()!=NULL&& Element2!=Element && Element2!=&TreeToCMP){

                                Element=&(Element->GetFather());
                                Element2=&(Element2->GetFather());  
                            }
                            Counter=1;
                        }while ((*Element)(LAST_USED).IsLastSon() && &(*Element).GetFather()!=NULL && &(*Element).GetFather()!=&this->GetFather());



                        if ((Element2==&TreeToCMP && ((*Element2)(LAST_USED).IsLastSon()))){

                            End=true;   
                        }
                    }

                    Depth--;
                }
            }   
            return true;
        }



        bool operator==(T ValueToCMP){

            if (Value==ValueToCMP) return true;
            else return false;
        }

        bool operator<(Tree<T> &TreeToCMP){

            if (Value<TreeToCMP.GetValue()) return true;
            else return false;
        }

        bool operator>(Tree<T> &TreeToCMP){

            if (*this<TreeToCMP || *this==TreeToCMP) return false;
            else return true;
        }

        bool operator<=(Tree<T> &TreeToCMP){

            if (*this<TreeToCMP || *this==TreeToCMP) return true;
            else return false;
        }

        bool operator>=(Tree<T> &TreeToCMP){

            if (*this==TreeToCMP || *this>TreeToCMP) return true;
            else return false;
        }

        bool operator!=(Tree<T> &TreeToCMP){

            if (*this==TreeToCMP) return false;
            else return true;
        }

        const Tree <T> &operator=(T Value){

            SetValue(Value);
            return *this;
        }

        const Tree <T> &operator=(Tree<T> &TreeToCopy){

            *this=TreeToCopy.GetValue();
            //Nodes=TreeToCopy.GetSonsList();

            SetSonsLimit(TreeToCopy.GetSonsLimit());

            int Counter=0;
            int Depth=0;
            bool End=false;

            Tree<T> *Element=this;
            Tree<T> *Element2=&TreeToCopy;

            while (End==false && Element2->GetNElems()>0){

                Depth++; 

                if (Counter==0){

                    Element->SetLastPosUsed(POS_UNDEFINED);
                    Element2->SetLastPosUsed(POS_UNDEFINED);

                    for (int i=0;i<Element2->GetNElems();i++){

                        Element->NewSon(RIGHT,(*Element2)(NEXT).GetValue());
                    }

                    Element2->SetLastPosUsed(POS_UNDEFINED);
                    Element->SetSonsLimit(Element2->GetSonsLimit());
                    Counter=1;
                }

                (*Element)(NEXT);
                (*Element2)(NEXT);

                if ((*Element2)(LAST_USED).GetNElems()>0){

                    Element=&(*Element)(LAST_USED);
                    Element2=&(*Element2)(LAST_USED);
                    Counter=0;
                }
                else{

                    if (((*Element2)(LAST_USED).IsLastSon())){

                        do{

                            Depth--;
                            if (&(*Element).GetFather()!=NULL && &(*Element2).GetFather()!=NULL && Element2!=Element && Element2!=&TreeToCopy){

                                Element=&(Element->GetFather());
                                Element2=&(Element2->GetFather());  
                            }
                            Counter=1;
                        }while ((*Element2)(LAST_USED).IsLastSon() && &(*Element2).GetFather()!=NULL && &(*Element).GetFather()!=&this->GetFather());

                        if ((Element2==&TreeToCopy && ((*Element2)(LAST_USED).IsLastSon()))){

                            End=true;   
                        }
                    }

                    Depth--;
                }
            }

            return *this;
        }

        T &operator[](int Index){

            if (Index==0 || Index==MYSELF) return this->GetValue();
            else return GetSon(Index).GetValue();
        }

        Tree<T> &operator()(int Position){

            if (Position==0 || Position==MYSELF) return *this;
            else return GetSon(Position);
        }

    private:

        Tree<T> *MyFather;

        T Value;
        int LastPosUsed;
        static int SonsLimit;
        List<Tree<T> > Nodes;        
};

template <class T> int Tree<T>::SonsLimit=UNDEFINED;
#include "TreeHeaders\TreeImplementation.h"

0 个答案:

没有答案