0

我有一个具有公共成员函数的 Node 对象。当我在这种情况下有一个指针(或双指针)指向原始对象时,如何调用成员函数?

这是 Node 类中的成员函数:

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

这是我试图调用该函数的地方:

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.

为了以防万一, .setMarked() 函数如下所示:

int Node::setMarked() {
    marked = 1;
    return marked;
}
4

1 回答 1

3

首先取消引用它两次。请注意, * 绑定不如.or紧密->,因此您需要括号:

(**s).setMarked();

或者,

(*s)->setMarked();

在您的原始代码中,编译器看到相当于

**(s.setMarked());

这就是为什么它不起作用。

于 2014-11-11T02:39:58.837 回答