Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:54:50+00:00 2026-05-23T18:54:50+00:00

I have a project where I’m implementing Dijkstra’s shortest path algorithm using C++ classes.

  • 0

I have a project where I’m implementing Dijkstra’s shortest path algorithm using C++ classes. It uses OpenGL but that is surely separate from my problems. I need some insight on what I’m doing wrong in my Dijkstra class method. Here is my 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 a path to target is 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)));
    return d;
}

bool Node::dijkstra(Node* graph[], Node *source, Node *target) {
    queue<Node> q;
    int i;
    for (i = 0; i < NUM_NODES; i++) {
        graph[i]->distance = INFIN;
    }
    source->distance = 0;   
    i = 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++;
    }

    return true;
}

My guess is that I’m using “->” and “.” operators all wrong. I get a lot of these errors when I try to compile:

error: base operand of ‘->’ has non-pointer type ‘Node’

My implementation of Dijkstra’s algorithm is somewhat retrofitted to meet my needs and is probably wrong, but I need this to compile so I can debug it.

The code listed is the only code giving me grief but if you would like to see another part of it just ask. A good explanation of what im doing wrong will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:54:51+00:00Added an answer on May 23, 2026 at 6:54 pm

    You should use the ‘->’ operator when accessing data through a pointer. Otherwise you should use the ‘.’ opearator.

    For example these following lines are identical

    Node *node = &q.front();
    
    (*node).link1->distance = 1;
    node1->link1->distance = 1;
    

    As for your compilation problems, the problem is with how you are accessing temp. temp is declaired as

    Node temp = q.front();
    

    Which means that temp is a copy of the node at the front of the queue and is not a pointer to it. This is why the compiler is complaining when you attempt to access it like it is a pointer. For example:

    temp->link1.distance = alt;
    

    should look like

    temp.link1->distance = alt;
    

    because temp is not a pointer but link1 is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that I'm currently working on but it currently only supports
I have project that I'm working on that is going to require a webserver.
I have a project that I would like to start beta testing soon, it
i have project in .net , i want to test it. But i dont
Say I have Project , that is in many-to-many association with Tag . I'm
Some of my .csproj project files have special target AssembleJS that merges all .js
I have a VS 2005/MSBuild 2.0 project (let's call it Project A) that I
I have a project that adds elements to an AutoCad drawing. I noticed that
I have project where i'm developing an Android App using a lot of existing
I have project that creates dlls. These dlls are invoked from python later by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.