0

When I am doing my C++ project on this linked list, one of the problems I face is getting this error message on the output terminal:

main.cpp:(.text+0x4d): undefined reference to 'insert(Node*&, int)'
collect2 error: 1d returned 1 exit status

So here is my code:

#include <iostream>
using namespace std;

struct Node
{
    int data;
    Node *link;
};
typedef Node* nodePtr;

void insert(nodePtr& head, int data);

int main()
{
   nodePtr head;
   head = new Node;
   head->data = 20;
   head->link = NULL;

   insert (head, 30);
   nodePtr tmp;
   tmp = head;

   while(tmp->link != NULL)
   {
       cout << tmp->data <<endl;
       tmp = tmp->link;
   }
}

But to no avail. I just do no know where I am getting it wrong for it to not run properly.

mch
  • 9,424
  • 2
  • 28
  • 42
  • You've provided a declaration of the function `insert(nodePtr& head, int data)` but no implementation of it. What do you expect your program to actually _do_ when it reaches that `insert(head, 30);` call? – Nathan Pierson Feb 01 '22 at 07:17
  • Your question, and in particularly the last part of it (starting with 'I tried adding lines of code like...') implies that you need to learn the basics before jumping to the implementation of stuff like linked lists. –  Feb 01 '22 at 07:24

0 Answers0