user2565010
user2565010

Reputation: 2030

C++ getting "invalid user-defined conversion" error

I have an error in the following C++ code:

class Node
{
    int value;
    Node *prev, *next;
public:
    Node(int value = 0, Node* prev = NULL, Node* next = NULL)
    {
        this->value = value;
        this->next = next;
        this->prev = prev;
    }
}


class LinkList
{
    Node head;

    void add(int value)
    {
        head = new Node(value, &head, NULL);
    }
}

I am getting an error inside the add function when I try to initialize the head.

error: invalid user-defined conversion from 'Node*' to 'const Node&' [-fpermissive]

Can anybody please help me with this?

Upvotes: 0

Views: 3694

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

In class LinkList change data member

Node head;

to

Node *head;

Upvotes: 0

Axel
Axel

Reputation: 14159

This would have been correct in Java:

    head = new Node(value, &head, NULL);

In C++ it is:

    head = Node(value, &head, NULL);

Upvotes: 2

Related Questions