1

How do I refer to the tail variable inside the take_input() function so that it reflects in the main ? Below are the main() and take_input() functions
Here I've tried to pass by reference [ **tail ] How do I refer to this inside the take_input() function for the linked list { *tail is causing an error }

Any help would be highly appreciated !!!!

node* take_input(node **tail)
    {
        
        node* head = NULL;
        int count = 0;
        string name;
 
        cout << "Enter the name of the president";
        cin >> name;

        while(!name.empty())
        { 
            
            count++ ;
            node *new_node = new node(name);
            if(head == NULL)
            {
                head = new_node;
                *tail = new_node;
            }
            else
            {
                *tail->next = new_node ;
                *tail= *tail->next;
            }

            cout << "Enter the name of a member , the secretary or NULL to exit";
            cin >> name ;
            
            
            
         }
               return head;

     }
        int main()
        {
            int n ;
            string name;
            node *tail = NULL;
       
        
           node *head =  take_input(&tail);
       
           cin >> n;
           cin >> name;

           node *headfinal = insert_node(head, n ,name);

           cout<< "\n Enter the member's name which needs to be deleted\n";
           string del;
           cin >> del;

           node *head_final2 = delete_node(headfinal,del, &tail);

           node *temp2 = head_final2 ;
       
           while(temp2 != NULL)
           {
               cout << temp2->name;
               temp2 = temp2->next;
           
           } 

This is the error I am currently facing:

expression must have a pointer to class type (c/c++) (131,5)

8
  • EASY on the ALL CAPS. It's YELLING. Also avoid adding emphasis in your code, as ** has meaning in C++ that isn't "bold". If you need to, add comments with // or /* ... */. Commented Nov 26, 2020 at 10:35
  • You can pass a reference to a pointer, i.e. node * take_input (node * & tail). Then in main, you can simply call node * head = take_input(tail);. Commented Nov 26, 2020 at 10:35
  • 2
    ⟼This code could benefit greatly by adopting an indentation style and applying it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it, and it can also make mistakes more obvious as they stand out visually. Commented Nov 26, 2020 at 10:36
  • 1
    In C++ use nullptr in preference to C's NULL. Also there's a strong convention to not have any space around ->, so use x->y to make it clear what's going on there. Commented Nov 26, 2020 at 10:37
  • 1
    @tadman Okay I'll be more careful next time Commented Nov 26, 2020 at 10:39

1 Answer 1

2

As answered by Thomas Sabnik in the comments:

-> has higher precedence than *. You have to use parenthesis:

Change *tail->next = new_node; to (*tail)->next = new_node; and *tail = *tail->next; to *tail = (*tail)->next;.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.