rodrigoalvesvieira
rodrigoalvesvieira

Reputation: 8062

Unable to compile C++ code: invalid conversion from 'Node*' to 'int'

I have this C++ class with an array of Node objects called adj (I guess you don't need to see the implementation of my Node class)

class Graph {
public:
    Node *adj;
    bool *marked;
    int nVertex, p;
    int *distance;

    void graph(int quantity);
    bool is_marked();

    void cleaner();
    void newVertex(int value);
};

And I have this method which creates a node nod and tries to store it in the p-th position in adj:

void Graph::newVertex(int value)
{
    Node *nod = new Node(value);
    adj[p++] = nod;
}

When I try to compile this code I get the following error message:

invalid conversion from 'Node*' to 'int'

I can't see what I have done wrong in my code. The array initialization looks right to me and the object assignment too. Please help me answer this question.

UPDATE: the code for the Node class:

class Node {
public:
    int value, cost;
    Node *next;

    Node() {}

    Node(int val) {
        value = val;
        next = NULL;
        cost = 0;
    }
};

UPDATE: I can't use C++ vector here. I'd love to but it's for a homework thing. Before anyone thinks I am cheating or something, please note that I'm not asking for a solution to my specific assigned problem, but instead to a problem that I am having for compiling the code.

Upvotes: 0

Views: 2808

Answers (4)

OK, with the definition of the Node class in hand I think I can see what you're trying to do. If I'm correct, you want to have Graph::adj point to a linked list of Node elements, with each Node then pointing to the next Node in the list. If that's correct the implementation of new_vertex needs to look something like:

void Graph::newVertex(int value)
  {
  Node *nod = new Node(value);
  nod->next = adj;
  adj = nod;
  }

No need to have an index (p) - you'd just walk the linked list of Node elements with code similar to

Node *n = adj;

while(n != NULL)
  {
  // do something useful with n

  n = n->next;
  }

If you really insist upon using array syntax to access elements of the linked list (bad idea in my mind, as it just adds to the potential confusion, but YMMV) you could add something like

Node *operator[](int n);  // 0-based index into Node list

to Graph with an implementation similar to

Node *operator[](int ndx)
  {
  Node *n = adj;

  for( ; n != NULL, ndx > 0 ; ndx--)
    n = n->next;

  return n;
  }

Share and enjoy,.

Upvotes: 1

reece
reece

Reputation: 8145

After you have created your new Node:

Node *nod = new Node(value);

you need to hook it up to your linked list. A linked list looks like:

[HEAD] => [value|next] => [value|next] => NULL

where HEAD in your case would be adj.

Therefore, you need to update the next of your new node to point to the current head node, then update the head node to point to the new node.

That is, you should end up with something like:

adj => [value|next] => [value|next] => ... => NULL
       ^               ^
       nod             adj'

where adj' is the old value of adj.

It helps to create diagrams of what the data structure looks like and how the values are being updated.

You then need to work out how to traverse the nodes in the list.

Also, don't forget to clean up the nodes in the Graph destructor (and be careful how you do that).

Upvotes: 1

Dietmar Kühl
Dietmar Kühl

Reputation: 153802

The type of adj[p++] is clearly Node& and you try to assign a Node* to it. I guess, your Node type has a constructor taking an int and the compiler tries but fails to convert the Node* to an int.

You probably meant to declare adj as

std::vector<Node*> adj;

... and then add new nodes using, e.g.:

adj.push_back(nod);

(note, that you still need to make sure the allocated objects get released at the appropriate point in time).

Upvotes: 7

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

adj is of type Node*. adj[someIndex] is then of type Node. You are trying to assign Node* to Node. My educated guess is, you have Node::operator=(int), so the compiler tries to interpret your code that way - but that doesn't work out either, producing the error message you observe.

Upvotes: 2

Related Questions