如何访问由另一个函数作为指针返回的对象的函数

How to access a function of an object that was returned as a pointer by another function.

本文关键字:函数 返回 对象 指针 另一个 何访问 访问      更新时间:2023-10-16

又是那个制作拼字游戏模拟器的家伙!这次我遇到的问题是,由于语法原因,函数被迫返回指向对象的指针。我想访问指针所指向的对象的函数。我该怎么做呢?我所涉及的函数代码如下。

boggleNode * nextNode(int adj){
    return adjacency[adj];              //For switching between nodes. 
}
bool getUsed(){
    return isUsed;
}
private:
boggleNode * adjacency[8];
char letter;
bool isUsed;
};

最后,这里是包含上述函数的函数:

int sift(boggleNode bog, string word, string matcher){
int endofnodes;
string matchee;
if (bog.getData() == 'q')
    matchee = matcher + bog.getData() + 'u';
else
    matchee = matcher + bog.getData();
if (compare(word, matcher) == 0){
    cout << word << endl;
    return 5;                                                 //If it finds the word, escape the loop. 
}
if (word.find(matcher) == 0){
bog.makeUsed();
for (int j = 0; j < 8; j++){
    if (bog.nextNode(j) != NULL){
        if ((bog.nextNode(j)).getUsed() == 0){
            endofnodes = sift(bog.nextNode(j), word, matchee);
            if (endofnodes == 5)
                return endofnodes;
            }
        }
    bog.reset();
    return 0;
    //If it doesn't find anything, move onto next adjacency. 
    /*any words share a common starting letter sequence with matcher*/
    //Sift using an adjacent node, and add the old string to the new one. 
    }
}
}

我特别要问这一行:

if ((bog.nextNode(j)).getUsed() == 0)

当我试图编译这段代码时,我得到" error: request for member "getUsed' in (&bog)->boggleNode::nextNode(j)',它是非class的输入' boggleNode*' "

非常感谢任何帮助。提前感谢!

您应该使用->而不是.:

if ((bog.nextNode(j))->getUsed() == 0)

在本例中是简写:

if ((*(bog.nextNode(j))).getUsed() == 0)