Jacob Varner
Jacob Varner

Reputation: 340

How to return a private pointer member variable

I am trying to implement a graph/maze of Node objects so that I could find the shortest path within the maze. So far everything works except for two pieces, both of which have to do with pointers. I thought I understood pointers, but C++ is still fairly new to me, so I might be doing something really dumb and any help would be greatly appreciated.

my Node class looks like this:

class Node {
public:
    ...
    Node *getAttachedNode(int index);
    Node *getPrevious();
    ...
private:
    ...
    Node *attachedNodes[4];
    Node *previous;
    ...
};

Now there is other stuff in the class too, but it is all working as I expected it too. And I was given everything in this class in the instructions for this project. Then I implemented these two functions in this way:

Node* Node::getAttachedNode(int index) { //EDIT: fixed the syntax
    return *attachedNodes[index];
}

Node* Node::getPrevious() { //EDIT: fixed the syntax
    return *previous;
}

EDIT: After correcting my syntax for the function calls, it fixed the original errors but now replaced them both with "cannot convert 'Node' to 'Node*' in return"

Upvotes: 1

Views: 373

Answers (1)

ravi
ravi

Reputation: 10733

Node Node::*getAttachedNode(int index) {      //wrong syntax for returning pointers 

should be change to

Node* Node::getAttachedNode(int index) {      //correct syntax

Also change

return *attachedNodes[index];

to

return attachedNodes[index];

if you want to return pointer.

Upvotes: 1

Related Questions