0

I am trying to write a program to print all elements of a linked list by recursion. I have used class for implementing the linked list. My program is:

struct node{
    int data;
    node* link;
};
class list{
    node* head;
    int size;
    public:
    list()
    {
        head=NULL;
        size=0;
    }
    void print(node *temp=head);
    void insertpos(int data, int pos);
};

void list::print(node *temp=head)
{
    if(temp==NULL)
    {cout<<"\n";
    return;}
    else
    {
        cout<<temp->data<<" ";
        print(temp->link);
    }
}

Compiler is giving message that the error is in definition of print function. I cannot figure out what is the problem. Please help me.

Edit:- The error I am getting is:

error: invalid use of non-static data member 'list::head'
   20 |     void print(node *temp=head);
      |                           ^~~~
list.cpp:9:11: note: declared here
    9 |     node* head;
      |           ^~~~
list.cpp:87:29: error: invalid use of non-static data member 'list::head'
   87 | void list::print(node *temp=head)
      |                             ^~~~
list.cpp:9:11: note: declared here
    9 |     node* head;
      |           ^~~~
list.cpp:87:6: error: default argument given for parameter 1 of 'void list::print(node*)' [-fpermissive]
   87 | void list::print(node *temp=head)
      |      ^~~~
list.cpp:20:10: note: previous specification in 'void list::print(node*)' here
   20 |     void print(node *temp=head);

The line number in the message is according to the complete program.

As suggested, I removed the default from the definition. But still, I am getting errors though less.

 error: invalid use of non-static data member 'list::head'
   87 | void list::print(node *temp=head)
      |                             ^~~~
list.cpp:9:11: note: declared here
    9 |     node* head;

The problem editor is showing is:

a non-static member reference must be relative to a specific object [20,7]
4
  • Can you copy-paste the exact compiler error here. Commented May 14, 2020 at 6:51
  • Please show a minimal reproducible example with the full error output from the compiler Commented May 14, 2020 at 6:51
  • Is it complaining about declaring default arguments in both the declaration and definition of print? You would remove the default from your definition Commented May 14, 2020 at 6:53
  • Do not use using namespace std within global namespace in production code (in quick sketches that is usually fine, though some odd UBs related to component implementation still may happen), in your particular case you barely dodged a problem here because there is an std::list. You can put using inside declaration or code blocks, just where you could use a type definition. Embedding using namespace std into another namespace may lead to ambiguity problems if that namespace would be "used" later Commented May 14, 2020 at 7:12

2 Answers 2

0

I think this issue is resolved by this: https://stackoverflow.com/a/20226907/13123504

If you are not able to find the source of the error, do use this post as a reference. Lemme know if it helps!

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

1 Comment

Thank you for looking into the problem. The code in the link is not using class and so its print function requires passing of head pointer during the call. But I am using class and I can use the head pointer as default argument and that's where I am getting the error.
0
void list::print(node *temp=head)

is an error, it should be

void list::print(node *temp)

Specify default arguments when you declare a function, not when you define it.

UPDATE

The other error is that you cannot use a member variable as a default argument. Instead you should have two versions of your function. Like this

class list{
    node* head;
    int size;
    public:
    list()
    {
        head=NULL;
        size=0;
    }
    void print() { print(head); }
    void print(node *temp);
    void insertpos(int data, int pos);
};

As you can see there are now two versions of print one that has no arguments, and one that has one argument. The no-arguments version just calls the one argument version with head as the parameter. Which is what you were trying to achieve before.

2 Comments

Thank you for looking into the problem. I tried both ways- default argument in declaration only and definition only but nothing works though I am getting lesser no. of errors.
@TusharAgrawal Yes, I should have spotted that. See update to answer.

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.