0

I have a Node object that has a public member function. When I have a pointer (or double pointer) in this case pointing to the original object, how do I call the member function?

Here is the member function in the Node class:

class Node {
public:
    ...
    int setMarked();
    ...
private:
    ...
    int marked;
    ...
};

And here is where I am trying to call that function:

Node **s;
s = &startNode; //startNode is the original pointer to the Node I want to "mark"
q.push(**s); //this is a little unrelated, but showing that it does work to push the original object onto the queue.
**s.setMarked(); //This is where I am getting the error and where most of the question lies.

And just in case it matters, the .setMarked() function looks like this:

int Node::setMarked() {
    marked = 1;
    return marked;
}
Jacob Varner
  • 340
  • 1
  • 4
  • 16
  • note that `q.push(**s);` will push a copy of the node ; your subsequent marking of `**s` won't affect the queue – M.M Nov 11 '14 at 03:12

1 Answers1

3

Dereference it twice first. Note that * binds less tightly than . or ->, so you need parens:

(**s).setMarked();

Or,

(*s)->setMarked();

In your original code, the compiler was seeing the equivalent of

**(s.setMarked());

which is why it wasn't working.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • This solves the original issue at hand, but for some reason now the push is not working. Is there any way this change would affect that? – Jacob Varner Nov 11 '14 at 02:44
  • 1
    Why did you change the push? This only affects calling a member function. – nneonneo Nov 11 '14 at 02:45
  • (Unless you're missing a semicolon after the push like your original code shows, in which case that's your problem) – nneonneo Nov 11 '14 at 02:45
  • I have the semicolon, and fixed it in the question, but the q.push(**s) call is throwing an error and it wasn't before I changed **s.setMarked() to (**s).setMarked(). – Jacob Varner Nov 11 '14 at 02:46
  • I have no idea. The syntax I posted is surely right for calling the member function. Without knowing how push is defined I cannot possibly tell you whats wrong. – nneonneo Nov 11 '14 at 02:48
  • It's just the standard .push() call for a queue. I think I found out what might me another issue, but thanks for the help. – Jacob Varner Nov 11 '14 at 02:49