Saad
Saad

Reputation: 3

cannot convert 'list*' to 'Node*' in assignment

I'm trying to construct an adjacency list. The code I wrote is as follows.

struct Node
{
    int dest;
    struct Node* next;  
};

struct list
{
    struct list *head;
};

The class is defined as:

class Graph
{
    private:
        int vertix;
        list *arr;
    public:
        Graph(int v)
        {
            vertix = v;
            arr = new list [vertix];
            for(int i=0;i<vertix;i++)
            {
                arr[i].head=NULL;
            }
        }

        Node* getNewNode(int destination)
        {
            Node* newNode = new Node;
            newNode->dest = destination;
            newNode->next = NULL;
            return newNode;
        }

The errors are in these functions:

        void addEdge(int src, int dest)
        {
            Node* newNode = getNewNode(dest);
            newNode->next = arr[src].head;
            arr[src].head = newNode;

            newNode = getNewNode(src);
            newNode->next = arr[dest].head;
            arr[dest].head = newNode;

        }
    void print()
    {
        cout<<"Adjacency list of vertix: "<<endl;
        for(int i = 0; i< vertix; i++)
        {
            Node *ptr = arr[i].head;
            cout<< i << "-->";
            while(ptr)
            {
                cout<< "-->"<<ptr->dest;
                ptr=ptr->next;
            }
            cout<<endl;
        }
    }    

  };

The error messages I get are: [Error] cannot convert 'list*' to 'Node*' in assignment [Error] cannot convert 'list*' to 'Node*' in initialization

Upvotes: 0

Views: 1436

Answers (1)

SingerOfTheFall
SingerOfTheFall

Reputation: 29976

I'm not sure if it is just a typo, but instead of

struct list
{
    struct list *head;
};

you should have

struct list
{
    Node *head;
};

since the head of a list is a node, not another list. This causes the error in this line:

Node *ptr = arr[i].head;

because you are trying to assign the head of the list (which in your current code is a list*) to a Node*

Upvotes: 1

Related Questions