0

public メンバー関数を持つ 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

最初に 2 回逆参照します。.* はorよりも結合が緩い->ため、括弧が必要であることに注意してください。

(**s).setMarked();

または、

(*s)->setMarked();

元のコードでは、コンパイラは同等のものを見ていました

**(s.setMarked());

それが機能しなかった理由です。

于 2014-11-11T02:39:58.837 に答える