samedi 6 novembre 2021

undefined error to template class function

I was trying implement an class template DoublyLinked list. I used to class here DoublyLinkde List and Double Node. And Implemented here inserts() and Display Function.
Can anybody help me to successfully insert and display operation.
For example i want to insert data by using insert function and want to see it by using display function.

The below is the code: I write this code in final.cpp file

template <class ItemType> //Reminder this is a template class, implement the .cpp accordingly
class DoubleNode
{
public:
    DoubleNode();
    //default constructor, sets next_ and prev_ to nullptr
    DoubleNode(const ItemType &anItem, DoubleNode<ItemType> *nextNodePtr = nullptr, DoubleNode<ItemType> *previousNodePtr = nullptr); //Parameterized Constructor, sets item_ to anItem, next_ to nextNodePtr, prev_ to previousNodePtr

    void setItem(const ItemType &anItem)
    {

        item_=anItem;
    }

    void setPrevious(DoubleNode<ItemType> *previousNodePtr)
    {
        next_= previousNodePtr;
    }


    void setNext(DoubleNode<ItemType> *nextNodePtr)
    {
        prev_=nextNodePtr;

    }

    ItemType getItem() const
    {

        return item_;
    }

    DoubleNode<ItemType> *getNext() const
    {

        return next_;
    }

    DoubleNode<ItemType> *getPrevious() const
    {
        return prev_;

    }

private:
    ItemType item_;              //actual content of your node
    DoubleNode<ItemType> *next_; //pointer to the node that is after this node
    DoubleNode<ItemType> *prev_; //pointer to the node that is before this node
};



template <class ItemType>
class DoublyLinkedList
{
public:
    // DoublyLinkedList(); //default constructor, sets headPtr_ to null and itemCount_ to 0

    /*
        Copy constructor that will make an exact copy of the list parameter, aList.
        This will make a deep copy of the list in the given parameter.
        You must iterate through the callee list parameter and insert nodes from aList into the caller list (the current one that isn't the parameter)
    */
    // DoublyLinkedList(const DoublyLinkedList &aList);

    // ~DoublyLinkedList(); //destructor that calls the clear function
    int getSize()
    {

        return itemCount_;

    } //return itemCount_

    /* *
        * Description: finds the node at parameter pos
        * Pre: pos is a valid place in the list, otherwise nullptr will be returned
        * Post: returns a pointer to the node at pos
    * */
    DoubleNode <ItemType> *getAtPos(const int &pos) const
    {
        DoubleNode<ItemType>* curPtr=headPtr_;
        for(int skip=1; skip<pos; skip++)
            curPtr=curPtr->getNext();
        return curPtr;
    }

    DoubleNode<ItemType> *getHeadPtr() const
    {

        return headPtr_;
    } //return *headPtr_

    /* *
        * Description: inserts parameter item in caller list at parameter position
            *IMPORTANT: Position 1 is the first position, not 0
        * Pre: position is a valid place within the list, otherwise false will be returned
        * Post: returns true if the item has been inserted in the caller list
        * Handles edge case of an invalid position parameter
        * Case: Inserting into head of the list
        * Case: Inserting into rear of the list
        * Case: Inserting into a position that is not an extremity
    * */

    bool inserts(const ItemType &item, const int &position = 1)
    {
        bool ableToInsert=(position>=1)&&(position<=itemCount_+1);
        if(ableToInsert)
        {
            DoubleNode<ItemType>* newNodePtr=new DoubleNode<ItemType>(item);
            if(position==1)
            {
                newNodePtr->setNext(headPtr_);
                headPtr_=newNodePtr;
            }
            else   if(position==2)
            {

                DoubleNode<ItemType>* prevPtr=getAtPos(position-1);
                newNodePtr->setNext(prevPtr->getNext());
                prevPtr->setNext(newNodePtr);
            }

            else
            {
                DoubleNode<ItemType>* headPtr_=getHeadPtr();
                DoubleNode<ItemType>* node = new DoubleNode<ItemType>[1];
//      cout<<"test 31"<<endl;
                node->setItem(item);
                if(headPtr_ == nullptr)
                {
                    // cout<<"test 1"<<endl;
                    headPtr_ = node;
                    // cout<<"new node added(firstnode) !"<<endl;
                    return true;
                }
                DoubleNode<ItemType>* temp = headPtr_;
                DoubleNode<ItemType>* prev_;
                // cout<<"test 1 "<<temp->next_<<endl;
                while(temp->getNext() != nullptr)
                {
                    //cout<<"test NULL 1"<<endl;
                    prev_ = temp;
                    temp = temp->getNext();
                }
                temp->setNext(node);
                temp->setPrevious(prev_);
                //cout<<"new node added at back!"<<endl;
            }
            itemCount_++;
        }

        return ableToInsert;
    }

    /* *
        * Description: removes node at parameter position
            *IMPORTANT: Position 1 is the first position, not 0
        * Pre: position is a valid place within the list, otherwise false will be returned
        * Post: returns true if the item at position has been removed from the caller list
        * Handles edge case of invalid position parameter
        * Case: removing only node in list
        * Case: removing from the end
        * Case: removing from the beginning
        * Case: removing from a position that is not an extremity
    * */
    // bool remove(const int &position);

    // bool isEmpty() const; //returns true if itemCount_ is 0

    /* *
        * Description: removes all items from the caller list
        * Traverse through the linked list and delete each individual node
        * Post: no nodes remain in the list and itemCount_ is at 0
    * */
    // void clear();

    //Iteratively outputs the contents of the caller list on the console
    //Example "A B C D E"
    //Note how theres no space at the end
    void display() const
    {
//     cout<<"linked list is empty"<<endl;
        DoubleNode <ItemType>*headPtr_=getHeadPtr();
        if(headPtr_ == nullptr)
        {
            // cout<<"linked list is empty"<<endl;
            return;
        }
        // cout<<endl<<"----link list items------"<<endl;
        DoubleNode<ItemType>* temp = headPtr_;
        while(temp != nullptr)
        {
            // cout<<temp->item_<<" ooo| ";
            temp = temp->getNext();
        }
        //  cout<<endl<<"--------------------------"<<endl;
    }

    //Iteratively outputs the contents of the caller list backwards on the console
    //Example if we had A B C D E we would instead display "E D C B A"
    //  void displayBackwards() const;

    // Reverses the list such that if my list was A B C D it will now be D C B A
    // Remember to change both previous and next pointers
    // Will be tested with both display and displayBackwards, make sure you have those working
    //  void invert();

private:
    DoubleNode<ItemType> *headPtr_; //points to the first item in the linked list
    int itemCount_;                 //lists how many items are currently in the list
};

int main()
{
    DoublyLinkedList<int> dl;

    dl.inserts(4,3);
    dl.display();
}

Aucun commentaire:

Enregistrer un commentaire