don't click here

Linked Lists == Pain in the Ass

Discussion in 'Technical Discussion' started by Travelsonic, Nov 21, 2011.

  1. Travelsonic

    Travelsonic

    Member
    827
    20
    18
    So I'm trying to make a templated linked list class.

    I have the node.h, node.template [which has the implementations for the node member functions], linkedList.h, and linkedList.template [containing linked list template functions].

    Problem is with the linkedList file.

    It is designed to take the place of having a linked list toolkit function set, or something where the actual implementations would be visible, and instead takes the node and encapsulates it within its own object.

    I have functions for inserting and removing nodes. For the removal, each function can either remove the node/eturn nothing, return the data as a reference, return the data as a pointer, return the node as a reference, or return the node as a pointer.

    I have the definitions down, and have empty shells - functions that have no code within them - in the implementation files as I haven't gotten to them yet.

    When I try to instantiate a linked list [and just instantiate it, do nothing to it yet since the functions haven't been fully implemented yet, these errors occur:

    ../linkedList.h: In instantiation of 'tLinkedList::linkedList<int>':
    ../main.cpp:11: instantiated from here
    ../linkedList.h:302: error: 'tNode::node<nData>* tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]' cannot be overloaded
    ../linkedList.h:295: error: with 'void tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]'
    ../linkedList.h:309: error: 'tNode::node<nData> tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]' cannot be overloaded
    ../linkedList.h:295: error: with 'void tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]'
    ../linkedList.h:317: error: 'nData* tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]' cannot be overloaded
    ../linkedList.h:295: error: with 'void tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]'
    ../linkedList.h:324: error: 'nData tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]' cannot be overloaded
    ../linkedList.h:295: error: with 'void tLinkedList::linkedList<nData>::removeAtHead() [with nData = int]'
    ../linkedList.h:342: error: 'tNode::node<nData>* tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]' cannot be overloaded
    ../linkedList.h:333: error: with 'void tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]'
    ../linkedList.h:351: error: 'tNode::node<nData> tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]' cannot be overloaded
    ../linkedList.h:333: error: with 'void tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]'
    ../linkedList.h:360: error: 'nData* tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]' cannot be overloaded
    ../linkedList.h:333: error: with 'void tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]'
    ../linkedList.h:369: error: 'nData tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]' cannot be overloaded
    ../linkedList.h:333: error: with 'void tLinkedList::linkedList<nData>::removeAtTail() [with nData = int]'
    ../linkedList.h:387: error: 'tNode::node<nData>* tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]' cannot be overloaded
    ../linkedList.h:378: error: with 'void tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]'
    ../linkedList.h:396: error: 'tNode::node<nData> tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]' cannot be overloaded
    ../linkedList.h:378: error: with 'void tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]'
    ../linkedList.h:405: error: 'nData* tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]' cannot be overloaded
    ../linkedList.h:378: error: with 'void tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]'
    ../linkedList.h:414: error: 'nData tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]' cannot be overloaded
    ../linkedList.h:378: error: with 'void tLinkedList::linkedList<nData>::removeAtPos(size_t) [with nData = int]'


    What the hell is going on?

    node.h
    Code (Text):
    1.  
    2. // Begin file node.h
    3.  
    4. // Preprocessor Directives:
    5. // Defining NODE_H to prevent naming conflicts
    6. #ifndef NODE_H
    7. #define NODE_H
    8.  
    9. // Libraries needed:
    10. #include <cstdlib>
    11. // End preprocessor directives
    12.  
    13. // Begin encapsulation into namespace tNode
    14. namespace tNode
    15. {
    16.    
    17.     // Begin class definition    
    18.     // Being a templated class, the "type" template name will be nData
    19.     template <typename nData>
    20.     class node
    21.     {
    22.      
    23.         // DATA:
    24.         private:
    25.             node * next;        // A pointer to a node representative of the
    26.                                 // next node in a linked list
    27.             node * prev;        // A pointer to a node representative of the
    28.                                 // previous node in a linked list
    29.             nData data;         // The data held in the node, uses template
    30.                                 // typename nData      
    31.      
    32.         // MEMBER FUNCTIONS:
    33.         public:
    34.              // CONSTRUCTORS:
    35.              // Constructor node();
    36.              // PRE:    None
    37.              // POST:   Creates a new node:
    38.              //         -- Next, prev [Node pointers] set to NULL
    39.              //         -- No value given for data;
    40.              node();
    41.              
    42.              // Constructor node(const nData inData);
    43.              // PRE:    Accepts a const nData inData
    44.              // POST:   Creates a new node:
    45.              //         -- Next, prev [Node pointers] set to NULL
    46.              //         -- Data is set to inData        
    47.              node(const nData inData);
    48.              
    49.              // COPY Constructor node(const node<nData> & inCopy);
    50.              // PRE:    Accepts a const reference node inCopy
    51.              // POST:   Creates a new node:
    52.              //         -- Sets next to inCopyNode's next node  
    53.              //         -- Sets prev to inCopyNode's prev node
    54.              //         -- Sets data to incopyNode's data value      
    55.              node(const node<nData> & inCopy);
    56.              
    57.              // COPY Constructor node(const node<nData> & inCopy);
    58.              // PRE:    Accepts a const node pointer inCopy
    59.              // POST:   Creates a new node:
    60.              //         -- Sets next to inCopyNode's next node  
    61.              //         -- Sets prev to inCopyNode's prev node
    62.              //         -- Sets data to incopyNode's data value  
    63.              node(const node<nData> * inCopy);
    64.              
    65.              // Constructor node(const node<nData> * inNext, const node<nData> * inPrev);
    66.              // PRE:    Accepts a const node pointer inNext, and a const node pointer inNext,
    67.              // POST:   Creates a new node:
    68.              //         -- Sets next inNext
    69.              //         -- Sets prev to inPrev
    70.              node(const node<nData> * inNext, const node<nData> * inPrev);
    71.              
    72.              // Constructor node(const node<nData> * inNext, const node<nData> * inPrev, const nData & inData);
    73.              // PRE:    Accepts a const node pointer inNext, a const node pointer inNext, and a const nDatareference inData
    74.              // POST:   Creates a new node:
    75.              //         -- Sets next inNext
    76.              //         -- Sets prev to inPrev
    77.              //         -- Sets data to inData
    78.              node(const node<nData> * inNext, const node<nData> * inPrev, const nData & inData);
    79.              
    80.              
    81.              
    82.              // DESTRUCTORS:
    83.              // Destructor ~node();
    84.              // PRE:    None
    85.              // POST:   Free the memory taken up by the node instance's
    86.              //         prev / next node pointers
    87.              ~node();
    88.              
    89.              // ACCESSORS:
    90.              // Accessor getNext() const;
    91.              // PRE:    None
    92.              // POST:   Returns next as a const node pointer
    93.              node<nData> * getNext() const;
    94.              
    95.              // ACCESSORS:
    96.              // Accessor getPrev() const;
    97.              // PRE:    None
    98.              // POST:   Returns prev as a const node pointer
    99.              node<nData> * getPrev() const;
    100.              
    101.              // ACCESSORS:
    102.              // Accessor getData() const;
    103.              // PRE:    None
    104.              // POST:   Returns data as const nData
    105.              nData getData() const;
    106.              
    107.              
    108.              
    109.              // MUTATORS:
    110.              // Mutator setNext(const node<nData> * inNext);
    111.              // PRE:    Accepts a const node pointer inNext
    112.              // POST:   Sets next to inNext
    113.              void setNext(const node<nData> * inNext);
    114.              
    115.              // Mutator setPrev(const node<nData> * inPrev);
    116.              // PRE:    Accepts a const node pointer inPrev
    117.              // POST:   Sets prev to inPrev
    118.              void setPrev(const node<nData> * inPrev);
    119.              
    120.              // Mutator setData(const nData & inData);
    121.              // PRE:    Accepts const nData reference inData
    122.              // POST:   Sets data to inData
    123.              void setData(const nData & inData);
    124.              
    125.              // Mutator setData(const nData * inData);
    126.              // PRE:    Accepts const nData pointer inData
    127.              // POST:   Sets data to inData
    128.              void setData(const nData * inData);
    129.              
    130.              
    131.              
    132.              // OVERLOADED OPERATORS:
    133.              // operator=(const node<nData> * inNode);
    134.              // PRE: Accepts a const node pointer inNode as the righthand argument
    135.              // POST: Sets this node so:
    136.              //       -- next = inNode's next node
    137.              //       -- prev = inNode's prev node
    138.              //       -- data = inNode's data value
    139.              void operator=(const node<nData> * inNode);
    140.              
    141.              // operator=(const node<nData> & inNode);
    142.              // PRE: Accepts a const node reference inNode as the righthand argument
    143.              // POST: Sets this node so:
    144.              //       -- next = inNode's next node
    145.              //       -- prev = inNode's prev node
    146.              //       -- data = inNode's data value
    147.              void operator=(const node<nData> & inNode);
    148.              
    149.              // operator==(const node<nData> * inNode);
    150.              // PRE: Accepts a const node pointer inNode as the righthand argument
    151.              // POST: Compares the data value of this node instance and inNode
    152.              //       -- Returns true if they are equal
    153.              //       -- Returns false if they are not
    154.              bool operator==(const node<nData> * inNode);  
    155.              
    156.              // operator==(const node<nData> & inNode);
    157.              // PRE: Accepts a const node reference inNode as the righthand argument
    158.              // POST: Compares the data value of this node instance and inNode
    159.              //       -- Returns true if they are equal
    160.              //       -- Returns false if they are not
    161.              bool operator==(const node<nData> & inNode);                
    162.  
    163.              // operator+=(const node<nData> * inNode);
    164.              // PRE: Accepts a const node pointer inNode as the righthand argument
    165.              // POST: If this instance's next node is NULL and inNode isn't
    166.              //       the same as this node instance, set next to inNode
    167.              void operator+=(const node<nData> * inNode);  
    168.              
    169.              // operator+=(const node<nData> & inNode);
    170.              // PRE: Accepts a const node reference inNode as the righthand argument
    171.              // POST: If this instance's next node is NULL and inNode isn't
    172.              //       the same as this node instance, set next to inNode
    173.              void operator+=(const node<nData> & inNode);
    174.              
    175.              
    176.              
    177.     };
    178.    
    179. // End encapsulation into namespace tNode
    180. }
    181.  
    182. // Include "node.template," which includes the function implementations for the
    183. // node class's member functions
    184. #include "node.template"
    185.  
    186. // End definition of NODE_H
    187. #endif
    188.  
    189. // End file node.h
    190.  
    191.  

    node.template
    Code (Text):
    1.  
    2. // Begin file node.template
    3.  
    4. // Preprocessor Directives:
    5. // Include node.h to get class, class member function definitions
    6. #include "node.h"
    7. // End preprocessor directives
    8.  
    9. // Begin encapsulation into namespace tNode
    10. namespace tNode
    11. {
    12.    
    13.      // Begin member function implementations
    14.  
    15.      // CONSTRUCTORS:
    16.      // Constructor node();
    17.      // PRE:    None
    18.      // POST:   Creates a new node:
    19.      //         -- Next, prev [Node pointers] set to NULL
    20.      //         -- No value given for data;
    21.      template <typename nData>
    22.      node<nData>::node()
    23.      {
    24.  
    25.          next = NULL;    // Set next to NULL
    26.          prev = NULL;    // Set prev to NULL
    27.        
    28.      }
    29.          
    30.      // Constructor node(const nData inData);
    31.      // PRE:    Accepts a const nData inData
    32.      // POST:   Creates a new node:
    33.      //         -- Next, prev [Node pointers] set to NULL
    34.      //         -- Data is set to inData      
    35.      template <typename nData>  
    36.      node<nData>::node(const nData inData)
    37.      {
    38.    
    39.          next = NULL;    // Set next to NULL
    40.          prev = NULL;    // Set prev to NULL
    41.          data = inData;  // Set data to inData
    42.  
    43.      }
    44.              
    45.      // COPY Constructor node(const node<nData> & inCopy);
    46.      // PRE:    Accepts a const reference node inCopy
    47.      // POST:   Creates a new node:
    48.      //         -- Sets next to inCopyNode's next node  
    49.      //         -- Sets prev to inCopyNode's prev node
    50.      //         -- Sets data to incopyNode's data value
    51.      template <typename nData>      
    52.      node<nData>::node(const node<nData> & inCopy)
    53.      {
    54.  
    55.          next = inCopy.getNext();       // Set next to inCopy's next node
    56.          prev = inCopy.getPrev();       // set prev to inCopy's prev node
    57.          data = inCopy.getData();       // Set data to inCopy's data value
    58.  
    59.      }
    60.              
    61.      // COPY Constructor node(const node<nData> & inCopy);
    62.      // PRE:    Accepts a const node pointer inCopy
    63.      // POST:   Creates a new node:
    64.      //         -- Sets next to inCopyNode's next node  
    65.      //         -- Sets prev to inCopyNode's prev node
    66.      //         -- Sets data to incopyNode's data value  
    67.      template <typename nData>
    68.      node<nData>::node(const node<nData> * inCopy)
    69.      {
    70.  
    71.          next = inCopy->getNext();       // Set next to inCopy's next node
    72.          prev = inCopy->getPrev();       // set prev to inCopy's prev node
    73.          data = inCopy->getData();       // Set data to inCopy's data value
    74.  
    75.      }
    76.  
    77.      // Constructor node(const node<nData> * inNext, const node<nData> * inPrev);
    78.      // PRE:    Accepts a const node pointer inNext, and a const node pointer inNext,
    79.      // POST:   Creates a new node:
    80.      //         -- Sets next inNext
    81.      //         -- Sets prev to inPrev
    82.      //         -- Nothing is done to data
    83.      template <typename nData>
    84.      node<nData>::node(const node<nData> * inNext, const node<nData> * inPrev)
    85.      {
    86.  
    87.         next = inNext;                   // Set next to inNext
    88.         prev = inPrev;                   // Set prev to inPrev
    89.  
    90.      }
    91.  
    92.              
    93.      // Constructor node(const node<nData> * inNext, const node<nData> * inPrev, const nData & inData);
    94.      // PRE:    Accepts a const node pointer inNext, a const node pointer inNext, and a const nDatareference inData
    95.      // POST:   Creates a new node:
    96.      //         -- Sets next inNext
    97.      //         -- Sets prev to inPrev
    98.      //         -- Sets data to inData
    99.      template <typename nData>
    100.      node<nData>::node(const node<nData> * inNext, const node<nData> * inPrev, const nData & inData)
    101.      {
    102.  
    103.         next = inNext;                   // Set next to inNext
    104.         prev = inPrev;                   // Set prev to inPrev
    105.         data = inData;                   // Set data to inData
    106.              
    107.      }  
    108.  
    109.    
    110.              
    111.      // DESTRUCTORS:
    112.      // Destructor ~node();
    113.      // PRE:    None
    114.      // POST:   Free the memory taken up by the node instance's
    115.      //         prev / next node pointers
    116.      template <typename nData>
    117.      node<nData>::~node()
    118.      {
    119.  
    120.         // Free up the memory used by next
    121.         delete next;
    122.         next = NULL;
    123.  
    124.         // Free up the memory used by prev
    125.         delete prev;
    126.         prev = NULL;
    127.  
    128.      }
    129.  
    130.  
    131.  
    132.      // ACCESSORS:
    133.      // Accessor getNext() const;
    134.      // PRE:    None
    135.      // POST:   Returns next as a const node pointer
    136.      template <typename nData>
    137.      node<nData> * node<nData>::getNext() const
    138.      {
    139.  
    140.          // Return next;
    141.          return next;
    142.  
    143.      }
    144.              
    145.      // Accessor getPrev() const;
    146.      // PRE:    None
    147.      // POST:   Returns prev as a const node pointer
    148.      template <typename nData>
    149.      node<nData> * node<nData>::getPrev() const
    150.      {
    151.  
    152.          // Return prev;
    153.          return prev;
    154.  
    155.      }
    156.  
    157.      // Accessor getData() const;
    158.      // PRE:    None
    159.      // POST:   Returns data as const nData
    160.      template <typename nData>
    161.      nData node<nData>::getData() const
    162.      {
    163.  
    164.          // Return data;
    165.          return data;
    166.  
    167.      }
    168.              
    169.              
    170.              
    171.      // MUTATORS:
    172.      // Mutator setNext(const node<nData> * inNext);
    173.      // PRE:    Accepts a const node pointer inNext
    174.      // POST:   Sets next to inNext
    175.      template <typename nData>
    176.      void node<nData>::setNext(const node<nData> * inNext)
    177.      {
    178.  
    179.          // Set next to inNext
    180.          next = inNext;
    181.  
    182.      }
    183.              
    184.      // Mutator setPrev(const node<nData> * inPrev);
    185.      // PRE:    Accepts a const node pointer inPrev
    186.      // POST:   Sets prev to inPrev
    187.      template <typename nData>
    188.      void node<nData>::setPrev(const node<nData> * inPrev)
    189.      {
    190.  
    191.          // Set next to inPrev
    192.          prev = inPrev;
    193.  
    194.      }
    195.              
    196.      // Mutator setData(const nData & inData);
    197.      // PRE:    Accepts const nData reference inData
    198.      // POST:   Sets data to inData
    199.      template <typename nData>
    200.      void node<nData>::setData(const nData & inData)
    201.      {
    202.  
    203.          // Set data to inData
    204.          data = inData;
    205.  
    206.      }
    207.              
    208.      // Mutator setData(const nData * inData);
    209.      // PRE:    Accepts const nData pointer inData
    210.      // POST:   Sets data to inData
    211.      template <typename nData>
    212.      void node<nData>::setData(const nData * inData)
    213.      {
    214.  
    215.          // Set data to inData
    216.          data = *(inData);
    217.  
    218.      }
    219.  
    220.  
    221.  
    222.      // OVERLOADED OPERATORS:
    223.      // operator =(const node<nData> * inNode);
    224.      // PRE: Accepts a const node pointer inNode as the righthand argument
    225.      // POST: Sets this node so:
    226.      //       -- next = inNode's next node
    227.      //       -- prev = inNode's prev node
    228.      //       -- data = inNode's data value
    229.      template <typename nData>
    230.      void node<nData>::operator=(const node<nData> * inNode)
    231.      {
    232.  
    233.          // Set next to inData's next node
    234.          next = inNode->getNext();
    235.  
    236.          // Set prev to inData's prev node
    237.          prev = inNode->getPrev();
    238.  
    239.          // Set data to inNode's data value
    240.          data = inNode->getData();
    241.  
    242.      }
    243.  
    244.      // operator =(const node<nData> & inNode);
    245.      // PRE: Accepts a const node reference inNode as the righthand argument
    246.      // POST: Sets this node so:
    247.      //       -- next = inNode's next node
    248.      //       -- prev = inNode's prev node
    249.      //       -- data = inNode's data value
    250.      template <typename nData>
    251.      void node<nData>::operator=(const node<nData> & inNode)
    252.      {
    253.  
    254.          // Set next to inData's next node
    255.          next = inNode.getNext();
    256.  
    257.          // Set prev to inData's prev node
    258.          prev = inNode.getPrev();
    259.  
    260.          // Set data to inNode's data value
    261.          data = inNode.getData();
    262.  
    263.      }
    264.  
    265.      // operator==(const node<nData> * inNode);
    266.      // PRE: Accepts a const node pointer inNode as the righthand argument
    267.      // POST: Compares the data value of this node instance and inNode
    268.      //       -- Returns true if they are equal
    269.      //       -- Returns false if they are not
    270.      template <typename nData>
    271.      bool node<nData>::operator==(const node<nData> * inNode)  
    272.      {
    273.  
    274.         // Return the boolean result of comparing data to inNode's data
    275.         return (data == inNode->getData());
    276.  
    277.      }
    278.  
    279.      // operator==(const node<nData> & inNode);
    280.      // PRE: Accepts a const node reference inNode as the righthand argument
    281.      // POST: Compares the data value of this node instance and inNode
    282.      //       -- Returns true if they are equal
    283.      //       -- Returns false if they are not
    284.      template <typename nData>
    285.      bool node<nData>::operator==(const node<nData> & inNode)          
    286.      {
    287.  
    288.         // Return the boolean result of comparing data to inNode's data
    289.         return (data == inNode.getData());
    290.  
    291.      }
    292.  
    293.      // operator+=(const node<nData> * inNode);
    294.      // PRE: Accepts a const node pointer inNode as the righthand argument
    295.      // POST: If this instance's next node is NULL and inNode isn't
    296.      //       the same as this node instance, set next to inNode
    297.      template <typename nData>
    298.      void node<nData>::operator+=(const node<nData> * inNode)
    299.      {
    300.  
    301.         // If this's next node is not null, return
    302.         if(next != NULL)
    303.         {
    304.  
    305.             return;
    306.  
    307.         }
    308.         // If inNode is this, return
    309.         if(inNode == this)
    310.         {
    311.  
    312.             return;
    313.  
    314.         }
    315.        
    316.         // Otherwise set this's next node to inNode
    317.         next = inNode;
    318.  
    319.      }
    320.  
    321.      // operator+=(const node<nData> & inNode);
    322.      // PRE: Accepts a const node reference inNode as the righthand argument
    323.      // POST: If this instance's next node is NULL and inNode isn't
    324.      //       the same as this node instance, set next to inNode
    325.      template <typename nData>
    326.      void node<nData>::operator+=(const node<nData> & inNode)
    327.      {
    328.  
    329.         if(next != NULL)
    330.         {
    331.  
    332.             return;
    333.  
    334.         }
    335.         if(inNode == this)
    336.         {
    337.  
    338.             return;
    339.  
    340.         }
    341.  
    342.         next = new node<nData>(inNode);
    343.  
    344.      }
    345.  
    346.  
    347.  
    348. // End class member function implementations
    349.    
    350. // End encapsulation into namespace tNode
    351. }
    352.  
    353. // End file node.template
    354.  
    355.  

    linkedList.h:
    Code (Text):
    1.  
    2. // Begin file "linkedList.h"
    3. // Begin preprocessor directives
    4. // Define LINKED_LIST_H to prevent naming conflicts
    5. #ifndef LINKED_LIST_H
    6. #define LINKED_LIST_H
    7.  
    8. // Include libraries we may need:
    9. #include "node.h"
    10.  
    11. // Using the tNode namespace since, even though we included node.h, we
    12. // need to explicitly say we need the node class encapsulated within it.
    13. using namespace tNode;
    14.  
    15. // End preprocessor directives
    16.  
    17. // Begin encapsulation into namespace tLinkedList
    18. namespace tLinkedList
    19. {
    20.  
    21.     // Begin class definition    
    22.     // Being a templated class, the "type" template name will be nData
    23.     template <typename nData>
    24.     class linkedList
    25.     {
    26.    
    27.         // DATA:
    28.         private:
    29.             size_t maxCapacity;         // The maximum capacity of the list
    30.             size_t currCapacity;        // The current capacity of the list
    31.            
    32.             bool isEmpty;               // BOOLEAN FLAG: Is the list empty?
    33.             bool isFull;                // BOOLEAN FLAG: Is the list full?
    34.            
    35.             node<nData> * lHead;        // The linked list head node
    36.             node<nData> * lTail;        // The linked list tail node
    37.            
    38.         // MEMBER FUNCTIONS:    
    39.         public:
    40.             // CONSTRUCTORS:
    41.             // Constructor linkedList()
    42.             // PRE:     None
    43.             // POST:    Create a new empty linked list:
    44.             //          -- lHead = NULL
    45.             //          -- lTail = NULL
    46.             //          -- maxCapacity = 0
    47.             //          -- currCapacity = 0
    48.             //          -- isEmpty = false
    49.             //          -- isFull = false
    50.            
    51.             linkedList();
    52.            
    53.             // COPY constructor linkedList(const linkedList<nData> * inList)
    54.             // PRE:     Accepts const linkedList pointer inList
    55.             // POST:    Create a new linkedList:
    56.             //          -- lHead = inList's lHead
    57.             //          -- lTail = inList's lTail
    58.             //          -- maxCapacity = inList's maxCapacity
    59.             //          -- currCapacity = inList's currCapacity
    60.             //          -- isEmpty = inList's isEmpty
    61.             //          -- isFull = inList's isFull
    62.             linkedList(const linkedList<nData> * inList);
    63.            
    64.             // COPY constructor linkedList(const linkedList<nData> & inList)
    65.             // PRE:     Accepts const linkedList reference inList
    66.             // POST:    Create a new linkedList:
    67.             //          -- lHead = inList's lHead
    68.             //          -- lTail = inList's lTail
    69.             //          -- maxCapacity = inList's maxCapacity
    70.             //          -- currCapacity = inList's currCapacity
    71.             //          -- isEmpty = inList's isEmpty
    72.             //          -- isFull = inList's isFull
    73.             linkedList(const linkedList<nData> & inList);
    74.    
    75.             // Constructor node(const node<nData> * inlistHead)
    76.             // PRE: Accepts const node pointer inList
    77.             // POST: Creates a new linkList:
    78.             //       -- lHead = inHead
    79.             //       -- lTail, currCapacity,isFull, isEmpty is set
    80.             //          based on traversing inHead
    81.             //       -- maxCapacity is set to currCapacity
    82.             linkedList(const node<nData> * inListHead);
    83.            
    84.             // Constructor node(const node<nData> & inlistHead)
    85.             // PRE: Accepts const node reference inList
    86.             // POST: Creates a new linkList:
    87.             //       -- lHead = inHead
    88.             //       -- lTail, currCapacity,isFull, isEmpty is set
    89.             //          based on traversing inHead
    90.             //       -- maxCapacity is set to currCapacity
    91.             linkedList(const node<nData> & inListHead);      
    92.            
    93.            
    94.            
    95.             // DESTRUCTORS:
    96.             // Destructor ~linkedList()
    97.             // PRE: None
    98.             // POST: Deallocates, frees up memory allocated to listHead, listTail
    99.             ~linkedList();
    100.            
    101.            
    102.            
    103.             // ACCESSORS:
    104.             // Accessor getMaxCapacity() const;
    105.             // PRE: None
    106.             // POST: Returns maxCapacity
    107.             size_t getMaxCapacity() const;
    108.            
    109.             // Accessor getCurrCapacity() const;
    110.             // PRE: None
    111.             // POST: Returns currCapacity
    112.             size_t getCurrCapacity() const;
    113.            
    114.             // Mutator calcCurrCapacity();
    115.             // PRE: None
    116.             // POST: Calculate currCapacity
    117.             void calcCurrCapacity();
    118.  
    119.             // Accessor getIsEmpty() const;
    120.             // PRE: None
    121.             // POST: Returns isEmpty
    122.             bool getIsEmpty() const;
    123.            
    124.             // Accessor getIsFull() const;
    125.             // PRE: None
    126.             // POST: Returns isFull
    127.             bool getIsFull() const;
    128.            
    129.             // Accessor getListHead() const;
    130.             // PRE: None
    131.             // POST: Returns list head; NULL if it isn't instantiated to anything
    132.             node<nData> * getListHead() const;
    133.            
    134.             // Accessor getListTail() const;
    135.             // PRE: None
    136.             // POST: Returns list head; NULL if it isn't instantiated to anything
    137.             node<nData> * getListTail() const;
    138.            
    139.             // Accessor getData(const size_t inPos, const nData inData) const;
    140.             // PRE: Accepts const size_t inPos that is not out of bounds
    141.             //      [<= 0 or >= maxCapacity or >= currCapacity], accepts const
    142.             //      nData inData
    143.             // POST: Traverses the list to find the data specified by nData
    144.             //       and returns it if it exists, returns NULL if it doesn't
    145.             nData getData(const size_t inPos, const nData inData) const;
    146.            
    147.  
    148.            
    149.             // MUTATORS:
    150.             // Mutator setMaxCapacity(const size_t inMaxCapacity);
    151.             // PRE:  Accepts a const size_t inMaxCapacity
    152.             // POST: Sets maxCapacity to inMaxCapacity
    153.             void setMaxCapacity(const size_t inMaxCapacity);
    154.            
    155.             // Mutator setCurrCapacity(const size_t inCurrCapacity);
    156.             // PRE: Accepts a const size_t inCurrCapacity
    157.             // POST: Returns currCapacity
    158.             void setCurrCapacity(const size_t inCurrCapacity);
    159.            
    160.             // Mutator setIsEmpty(const bool inIsEmpty);
    161.             // PRE: Accepts const bool inIsEmpty
    162.             // POST: sets isEmpty to inIsEmpty;
    163.             void setIsEmpty(const bool inIsEmpty);
    164.            
    165.             // Mutator calcIsEmpty();
    166.             // PRE: None
    167.             // POST: Calculates / sets isEmpty
    168.             void calcIsEmpty();
    169.                
    170.             // Mutator setIsFull(const bool inIsFull);
    171.             // PRE: Accepts const bool inIsFull
    172.             // POST: Sets isFull to inIsFull
    173.             void setIsFull(const bool inIsFull);
    174.  
    175.             // Mutator calcIsFull()
    176.             // PRE:  None
    177.             // POST: Calculates / sets isFull
    178.             void calcIsFull();
    179.            
    180.             // Mutator setListHead(const node<nData> inListHead);
    181.             // PRE: accepts const node pointer inListHead
    182.             // POST: listHead is set to inListHead, maxCapacity, currCapacity recalculated
    183.             //       and listTail recalculated
    184.             void setListHead(const node<nData> * inListHead);
    185.            
    186.             // Mutator setListTail();
    187.             // PRE: None
    188.             // POST: Takes listHead, and traverses it to find the end, which is
    189.             //       what listTail takes.
    190.             void setListTail();
    191.            
    192.             // Mutator addAtHead();
    193.             // PRE: None
    194.             // POST: Inserts a new empty node at the head
    195.             //       or if listHead is NULL, sets listHead to a new empty node
    196.             void addAtHead();
    197.            
    198.             // Mutator addAtHead(const node<nData> * inNode);
    199.             // PRE: Accepts a const node pointer inNode
    200.             // POST: Inserts inNode into the linked list at the head, or if the
    201.             //       list is NULL, sets listHead to inNode
    202.             void addAtHead(const node<nData> * inNode);
    203.            
    204.             // Mutator addAtHead(const node<nData> & inNode);
    205.             // PRE: Accepts a const node reference inNode
    206.             // POST: Inserts inNode into the linked list at the head, or if the
    207.             //       list is NULL, sets listHead to inNode
    208.             void addAtHead(const node<nData> & inNode);
    209.            
    210.             // Mutator addAtHead(const nData * inData);
    211.             // PRE: Accepts const nData pointer inData
    212.             // POST: Adds a new node at the head of the linked list with inData
    213.             //       as the data member value, OR if listHead is NULL, sets
    214.             //       it to a new node with inData as its data member value
    215.             void addAtHead(const nData * inData);
    216.            
    217.             // Mutator addAtHead(const nData & inData);
    218.             // PRE: Accepts const nData referencd inData
    219.             // POST: Adds a new node at the head of the linked list with inData
    220.             //       as the data member value, OR if listHead is NULL, sets
    221.             //       it to a new node with inData as its data member value
    222.             void addAtHead(const nData & inData);
    223.            
    224.             // Mutator addAtTail();
    225.             // PRE: None
    226.             // POST: Adds a new empty node after the tail pointer, sets the
    227.             //       tail pointer listTail to the newly created node
    228.             void addAtTail();
    229.            
    230.             // Mutator addAtTail(const node<nData> * inNode);
    231.             // PRE: Accepts a const node pointer inNode
    232.             // POST: Inserts inNode into the linked list at the tail, setting
    233.             //       the tail pointer to inNode
    234.             void addAtTail(const node<nData> * inNode);
    235.            
    236.             // Mutator addAtTail(const node<nData> & inNode);
    237.             // PRE: Accepts a const node reference inNode
    238.             // POST: Inserts inNode into the linked list at the tail, setting
    239.             //       the tail pointer to inNode
    240.             void addAtTail(const node<nData> & inNode);
    241.            
    242.             // Mutator addAtTail(const nData * inData);
    243.             // PRE: Accepts const nData pointer inData
    244.             // POST: Adds a new node at the tail of the linked list with inData
    245.             //       as the data member, and sets the tail pointer to inNode
    246.             void addAtTail(const nData * inData);
    247.            
    248.             // Mutator addAtTail(const nData & inData);
    249.             // PRE: Accepts const nData reference inData
    250.             // POST: Adds a new node at the tail of the linked list with inData
    251.             //       as the data member, and sets the tail pointer to inNode
    252.             void addAtTail(const nData & inData);
    253.            
    254.             // Mutator addAtPos(const size_t inPos);
    255.             // PRE: Accepts const size_t inPos that is not out of bounds
    256.             //      [<= 0 or >= maxCapacity]
    257.             // POST: Creates an empty node at inPos, linking it to nodes
    258.             //       that sequentially follow it in the list
    259.             void addAtPos(const size_t inPos);
    260.    
    261.             // Mutator addAtPos(const node<nData> * inNode, const size_t inPos);
    262.             // PRE: Accepts const node pointer inNode, accepts size_t inPos that
    263.             //      is not out of bounds [<= 0 or >= maxCapacity]
    264.             // POST: Inserts inNode at inPos, linking it to nodes
    265.             //       that sequentially follow it in the list.
    266.             void addAtPos(const node<nData> * inNode, const size_t inPos);
    267.  
    268.             // Mutator addAtPos(const node<nData> & inNode, const size_t inPos);
    269.             // PRE: Accepts const node reference inNode, accepts size_t inPos that
    270.             //      is not out of bounds [<= 0 or >= maxCapacity]
    271.             // POST: Inserts inNode at inPos, linking it to nodes
    272.             //       that sequentially follow it in the list.
    273.             void addAtPos(const node<nData> & inNode, const size_t inPos);
    274.  
    275.             // Mutator addAtPos(const nData * inData, const size_t inPos);
    276.             // PRE: Accepts const nData pointer inData, accepts size_t inPos that
    277.             //      is not out of bounds [<= 0 or >= maxCapacity]
    278.             // POST: Inserts a new node at inPos, setting its data member value
    279.             //       to inData, and linking the node to nodes that sequentially
    280.             //       follow it in the list.
    281.             void addAtPos(const nData * inData, const size_t inPos);
    282.  
    283.             // Mutator addAtPos(const nData & inData, const size_t inPos);
    284.             // PRE: Accepts const nData reference inData, accepts size_t inPos that
    285.             //      is not out of bounds [<= 0 or >= maxCapacity]
    286.             // POST: Inserts a new node at inPos, setting its data member value
    287.             //       to inData, and linking the node to nodes that sequentially
    288.             //       follow it in the list.
    289.             void addAtPos(const nData & inData, const size_t inPos);
    290.            
    291.             // Mutator removeAtHead();
    292.             // PRE:  None
    293.             // POST: Removes node at the head, returns nothing.
    294.             //       Sets listHead to NULL if it was the last node
    295.             //       in the list
    296.             void removeAtHead();
    297.            
    298.             // Mutator removeAtHead()
    299.             // PRE:  None
    300.             // POST: Removes node at the head, returns the node itself as
    301.             //       a pointer.  Sets listHead to NULL if it was the last node
    302.             //       in the list
    303.             node<nData> * removeAtHead();
    304.  
    305.             // Mutator removeAtHead()
    306.             // PRE:  None
    307.             // POST: Removes node at the head, returns the node itself as
    308.             //       a reference.  Sets listHead to NULL if it was the last node
    309.             //       in the list
    310.             node<nData> & removeAtHead();
    311.            
    312.            
    313.             // Mutator removeAtHead();
    314.             // PRE:  None
    315.             // POST: Removes node at the head, returns the data itself as
    316.             //       a nData pointer.  Sets listHead to NULL if it was the last
    317.             //       node in the list
    318.             nData * removeAtHead();
    319.            
    320.             // Mutator removeAtHead();
    321.             // PRE:  None
    322.             // POST: Removes node at the head, returns the data itself as
    323.             //       a nData reference.  Sets listHead to NULL if it was the last
    324.             //       node in the list
    325.             nData & removeAtHead();
    326.            
    327.             // Mutator removeAtTail()
    328.             // PRE:  None
    329.             // POST: Removes node at the tail, nothing is returned,
    330.             //       making the node previous to it the tail
    331.             //       node if it exists, or if the tail pointer is the same as the
    332.             //       head pointer, sets it to NULL if it is the last node in
    333.             //       the list
    334.             void removeAtTail();
    335.            
    336.             // Mutator removeAtTail()
    337.             // PRE:  None
    338.             // POST: Removes node at the tail, returns the node itself as
    339.             //       a pointer, making the node previous to it the tail
    340.             //       node if it exists, or if the tail pointer is the same as the
    341.             //       head pointer, sets it to NULL if it is the last node in
    342.             //       the list
    343.             node<nData> * removeAtTail();
    344.            
    345.             // Mutator removeAtTail()
    346.             // PRE:  None
    347.             // POST: Removes node at the tail, returns the node itself as
    348.             //       a reference, making the node previous to it the tail
    349.             //       node if it exists, or if the tail pointer is the same as the
    350.             //       head pointer, sets it to NULL if it is the last node in
    351.             //       the list
    352.             node<nData> & removeAtTail();
    353.            
    354.             // Mutator removeAtTail();
    355.             // PRE:  None
    356.             // POST: Removes node at the tail, returns the data itself as
    357.             //       a nData pointer, making the node previous to it the tail
    358.             //       node if it exists, or if the tail pointer is the same as the
    359.             //       head pointer, sets it to NULL if it is the last node in
    360.             //       the list
    361.             nData * removeAtTail();
    362.            
    363.             // Mutator removeAtTail();
    364.             // PRE:  None
    365.             // POST: Removes node at the tail, returns the data itself as
    366.             //       a nData reference, making the node previous to it the tail
    367.             //       node if it exists, or if the tail pointer is the same as the
    368.             //       head pointer, sets it to NULL if it is the last node in
    369.             //       the list
    370.             nData & removeAtTail();      
    371.            
    372.             // Mutator removeAtPos(const size_t inPos);
    373.             // PRE: Accepts const size_t inPos that is not out of bounds
    374.             //      [<= 0 or >= maxCapacity or >= currCapacity]
    375.             // POST: Removes the node at inPos;  If it is the tail pointer, setting
    376.             //       the one before it as the tail pointer, or if it is the last
    377.             //       node in the list, listHead becomes NULL
    378.             //       Nothing is returned.
    379.             void removeAtPos(const size_t inPos);
    380.            
    381.             // Mutator removeAtPos(const size_t inPos);
    382.             // PRE: Accepts const size_t inPos that is not out of bounds
    383.             //      [<= 0 or >= maxCapacity or >= currCapacity]
    384.             // POST: Removes the node at inPos;  If it is the tail pointer, setting
    385.             //       the one before it as the tail pointer, or if it is the last
    386.             //       node in the list, listHead becomes NULL
    387.             //       The node is returned as a node pointer
    388.             node<nData> * removeAtPos(const size_t inPos);
    389.            
    390.             // Mutator removeAtPos(const size_t inPos);
    391.             // PRE: Accepts const size_t inPos that is not out of bounds
    392.             //      [<= 0 or >= maxCapacity or >= currCapacity]
    393.             // POST: Removes the node at inPos;  If it is the tail pointer, setting
    394.             //       the one before it as the tail pointer, or if it is the last
    395.             //       node in the list, listHead becomes NULL
    396.             //       The node is returned as a node reference
    397.             node<nData> & removeAtPos(const size_t inPos);
    398.            
    399.             // Mutator removeAtPos(const size_t inPos);
    400.             // PRE: Accepts const size_t inPos that is not out of bounds
    401.             //      [<= 0 or >= maxCapacity or >= currCapacity]
    402.             // POST: Removes the node at inPos;  If it is the tail pointer, setting
    403.             //       the one before it as the tail pointer, or if it is the last
    404.             //       node in the list, listHead becomes NULL
    405.             //       The data is returned as a nData pointer
    406.             nData * removeAtPos(const size_t inPos);
    407.            
    408.             // Mutator removeAtPos(const size_t inPos);
    409.             // PRE: Accepts const size_t inPos that is not out of bounds
    410.             //      [<= 0 or >= maxCapacity or >= currCapacity]
    411.             // POST: Removes the node at inPos;  If it is the tail pointer, setting
    412.             //       the one before it as the tail pointer, or if it is the last
    413.             //       node in the list, listHead becomes NULL
    414.             //       The data is returned as a nData reference
    415.             nData & removeAtPos(const size_t inPos);
    416.            
    417.     };
    418.    
    419. }
    420.  
    421. // End encapsulation into namespace tLinkedList
    422.  
    423. // Include "node.template," which includes the function implementations for the
    424. // node class's member functions
    425. #include "linkedList.template"
    426.  
    427. // End definition of LINKED_LIST__H
    428. #endif
    429.  
    430. // End file linkedList.h
    431.  
     
  2. FraGag

    FraGag

    Tech Member
    You're trying to overload on the return type alone, which isn't legal. That is, you cannot have two member functions that have the same names and the same parameter types, regardless of the return type.
     
  3. Travelsonic

    Travelsonic

    Member
    827
    20
    18
    Awww damn. Oh well. Problem solved.
     
  4. sasuke

    sasuke

    Member
    66
    0
    0
    I think the problem is that you are overloading functions with the same parameters but different return types, which is not possible in C++. There is a Stack Overflow entry about this.

    EDIT: TOO LATE! :v:
     
  5. Travelsonic

    Travelsonic

    Member
    827
    20
    18
    Alright, new conundrum:

    My node class works perfectly now.

    My linked list class is on its way to being that way, but I have some MAJOR issues.


    I have a head pointer, a tail pointer, a boolean value for when it is empty, a boolean value for when it is full, an integer value for the maximum capacity, and an integer for the current capacity.

    I right now implemented adding nodes - empty, or existing nodes - at the head of the list, which works perfectly. [haven't implemented anything else YET because of the current issues]. A function I have that traverses it to calculate the current capacity can do so with NO problems at all.

    When I try to set the tail pointer, or remove nodes at the head of the list, it causes the program using it to crash.

    NODE REMOVAL [AT HEAD] FUNCTION:

    Code (Text):
    1.  
    2.             // Mutator removeAtHead();
    3.             // PRE:  None
    4.             // POST: Removes node at the head, returns nothing.
    5.             //       Sets listHead to NULL if it was the last node
    6.             //       in the list
    7.             template <typename nData>
    8.             void linkedList<nData>::removeAtHead()
    9.             {
    10.                  
    11.          // Check to see if we are trying to remove an empty node from the list.
    12.          // Return if lHead is NULL, otherwise continue.
    13.                  if(lHead == NULL)
    14.                  {
    15.                          
    16.                      return;
    17.                      
    18.                  }
    19.                  else
    20.                  {
    21.                      
    22.                      // Create a temporary node
    23.                      node<nData> * removeNode;
    24.                      
    25.                      // Set that temporary node to lHead
    26.                      removeNode = lHead;
    27.                      
    28.                      // Set lHead to the next node it points to
    29.                      lHead = lHead->getNext();
    30.                      
    31.                      // Free the memory allocated to removeNode - delete it
    32.                      delete removeNode;
    33.                      
    34.                      // Decrement currCapacity
    35.                      currCapacity--;
    36.                      
    37.                      // Calculate isEmpty
    38.                      calcIsEmpty();
    39.                      
    40.                  }
    41.                  
    42.             }
    43.  

    [ASIDE from encapsulation into an if/els conditional, and the decrement of currCapacity/recalculation of isEmpty, it is copied out of the textbook I use.


    TAIL SETTING FUNCTION:
    Code (Text):
    1.  
    2.             // Mutator setListTail();
    3.             // PRE: None
    4.             // POST: Traverses the list recursively until it reaches
    5.             //       the end - or when the next node is NULL.
    6.             //       When this happens, the last node has been reached, and lTail
    7.             //       is set to cursor
    8.             template <typename nData>
    9.             void linkedList<nData>::setListTail()
    10.             {
    11.                  
    12.                  // Create a temporary node, cursor, for traversing the list
    13.                  node<nData> * cursor;
    14.                  
    15.                  // Traverse the list until cursor is NULL
    16.                  for(cursor = lHead; cursor != NULL; cursor = cursor->getNext())
    17.                  {
    18.                  
    19.                      // BUT if cursor's next link IS NULL, then we've found
    20.                      // the end - where our tail is
    21.                      if(cursor->getNext() == NULL)
    22.                      {
    23.                          
    24.                          // Set lTail to cursor                
    25.                          lTail = cursor;                    
    26.                                          
    27.                      }          
    28.                            
    29.                  }
    30.  
    31.          // Free up memory used by cursor
    32.              delete cursor;
    33.                  
    34.             }
    35.  


    I feel like a goddammed moron.


    EDIT: nevermind? It works perfectly compiled on mac OS X using NetBeans IDE and G++, but crashed using the MinGW compiler, Dev-C++ IDE?

    Now I really feel dumb. :rolleyes:
     
  6. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    You might still have a very subtle problem that for some reason only the gcc Dev-C++ is using is showing. Try running the program in the Dev-C++ debugger?