Append won't work on Linked List of Arrays in C ++

This is my class

class NumberList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      double value[10];           // The value in this node
      struct ListNode *next;  // To point to the next node
   }; 

   ListNode *head;            // List head pointer

public:
   // Constructor
   NumberList()
      { head = nullptr; }

   // Destructor
   ~NumberList();

   // Linked list operations
   void appendNode(double []);
   void insertNode(double []);
   void deleteNode(double []);
   void displayList() const;
};

      

This is my append function and I can't seem to get it to work - I keep getting the error.

void NumberList::appendNode(double num[])
{
   ListNode *newNode;  // To point to a new node
   ListNode *nodePtr;  // To move through the list

   // Allocate a new node and store num there.
   newNode = new ListNode;
   newNode->value = num;
   newNode->next = nullptr;

   // If there are no nodes in the list
   // make newNode the first node.
   if (!head)
      head = newNode;
   else  // Otherwise, insert newNode at end.
   {
      // Initialize nodePtr to head of list.
      nodePtr = head;

      // Find the last node in the list.
      while (nodePtr->next)
         nodePtr = nodePtr->next;

      // Insert newNode as the last node.
      nodePtr->next = newNode;
   }
}

      

Error message:

prog.cpp: In member function โ€˜void NumberList::appendNode(double*)โ€™: prog.cpp:40:19: error: incompatible types in assignment of โ€˜double*โ€™ to โ€˜double [10]โ€™ newNode->value = num;

      

Any suggestions on what I am doing wrong?

+3


source to share


1 answer


The type of the parameter num

in is void NumberList::appendNode(double num[])

indeed a pointer (= double*

), not an array with a certain number of elements.

Using std::array<double,10>

in your framework and as a parameter appendNode

would be a good solution.

It:

struct ListNode
{
  double value[10];
...

      

becomes:



struct ListNode
{
  std::array<double,10> value;
...

      

And your function parameters will be declared as:

void appendNode(const std::array<double,10>& num);

      

newNode->value = num;

does not require changes.

+6


source







All Articles