Reputation: 210
I am doing PrimSparse algorithm and it says I need two indices in my for loop
private Node z;
adj = new int[V + 1, V + 1];
Node t;
for (t = adj[v]; t != z; t = t.next)
{
if (t.wgt < dist[u])
{
//puts the distance between the two vertex as the current data
dist[v] = t.wgt;
parent[u] = v;
//making sure when the vertex is == 0 then insert the vertex
if (hPos[u] == 0)
{
h.insert(t.vert);
}
else
{
//or otherwise just move up the position of the vertex
h.siftUp(hPos[t.vert]);
}
}
}
the very first line is giving me the CS0022 error, absolutely no idea why, when i put in a second it says "cannot convert int to Graph.node"
any ideas?
Upvotes: 0
Views: 3737
Reputation: 3034
The answer is relatively straightforward:
CS0022 is "Wrong number of indices inside []"
adj is declared as a 2D array of ints, so you will need to supply two indices when you dereference it, not one. This is just how arrays work.
You seem to have correctly diagnosed that issue - so onto the 2nd:
Dereferencing an adj gives you an int. You're then trying to assign an int to t, which has a type of "node" There's no conversion defined between "int" and "node", thus the 2nd error message. You can read it as straight English, "Cannot convert an int to a node" - but you have to make one more logical step. Why is a conversion from int to node necessary? Because you're trying to do assignment. Maybe a more verbose error message would be "Cannot assign an int to a variable of type Node because there is no conversion specified"
Some Lurking issues:
Comparing T and Z should be no problem. They're both Nodes. You may need to provide your own equality operator though. By default, != is going to do a reference compare. You may or may not want to compare references.
Finally, assuming the type of t.next is also a node, the last part of your for loop should be OK too.
Upvotes: 2