class - help with c++ classes, pointers n such -
class - help with c++ classes, pointers n such -
i have project i'm implementing dijkstra's shortest path algorithm using c++ classes. uses opengl certainly separate problems. need insight on i'm doing wrong in dijkstra class method. here relevant code:
class node { public: glfloat x, y, z; int numlinks; node *link1; node *link2; glfloat distance; node *previous; node(glfloat x, glfloat y, node *link1, node *link2); node(glfloat x, glfloat y, node *link1); node(); node(glfloat x, glfloat y); ~node(); bool dijkstra(node* graph[], node *source, node *target); //returns true if path target found int dist(node &n1, node &n2); }; int node::dist(node &n1, node &n2) { glfloat d = sqrt((pow((n2.x - n1.x), 2)) + (pow((n2.y - n1.y), 2))); homecoming d; } bool node::dijkstra(node* graph[], node *source, node *target) { queue<node> q; int i; (i = 0; < num_nodes; i++) { graph[i]->distance = infin; } source->distance = 0; = 0; q.push(*source); while (!q.empty()) { node temp = q.front(); glfloat d1 = dist(temp, temp->link1); glfloat d2 = dist(temp, temp->link2); temp.link1.distance = d1; temp.link1.distance = d2; glfloat alt = temp.distance + temp->link1.distance; if (alt < temp->link1.distance) { temp->link1.distance = alt; temp->previous = temp; } alt = temp->distance + temp->link2->distance; if (alt < temp->link2->distance) { temp->link2->distance = alt; temp->previous = temp; } if(d1 > d2) { q.push(temp->link2); q.push(temp->link1); } else { q.push(temp->link1); q.push(temp->link2); } q.pop(); i++; } homecoming true; }
my guess i'm using "->" , "." operators wrong. lot of these errors when seek compile:
error: base of operations operand of ‘->’ has non-pointer type ‘node’
my implementation of dijkstra's algorithm retrofitted meet needs , wrong, need compile can debug it.
the code listed code giving me grief if see part of ask. explanation of im doing wrong appreciated.
you should utilize '->' operator when accessing info through pointer. otherwise should utilize '.' opearator.
for illustration these next lines identical
node *node = &q.front(); (*node).link1->distance = 1; node1->link1->distance = 1;
as compilation problems, problem how accessing temp. temp declaired
node temp = q.front();
which means temp copy of node @ front end of queue , not pointer it. why compiler complaining when effort access pointer. example:
temp->link1.distance = alt;
should like
temp.link1->distance = alt;
because temp not pointer link1 is.
c++ class pointers
Comments
Post a Comment