尝试遍历 C++ 中的对象指针队列

Trying to iterate through queue of object pointers in c++

我正在尝试遍历 class Node 的对象的双端队列,并从每个对象调用函数 get_State。代码如下:

 #include <deque>
#include <queue>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
#include <sys/time.h>

#define  MAX_LINE_LENGTH 999 

class Node {
private:
    state_t base_state, new_state;
    int g_score;

public:
    Node (state_t base) {
        base_state=base;
        g_score=-1;

    }

    state_t* getState() {
        return &base_state;
    }

};

int main( int argc, char **argv )
{
// VARIABLES FOR INPUT
    char str[ MAX_LINE_LENGTH +1 ] ;
    ssize_t nchars; 
    state_t state; // state_t is defined by the PSVN API. It is the type used for individual states.

    bool goalStateEncountered=false;
    bool closedStateEncountered=false;  

// VARIABLES FOR ITERATING THROUGH state's SUCCESSORS
    state_t child;
    ruleid_iterator_t iter; // ruleid_terator_t is the type defined by the PSVN API successor/predecessor iterators.
    int ruleid ; // an iterator returns a number identifying a rule
    int nodeExpansions=0; 
    int childCount=0;
    int current_g=0;

// READ A LINE OF INPUT FROM stdin
    printf("Please enter the start state followed by Enter: ");
    if ( fgets(str, sizeof str, stdin) == NULL ) {
    printf("Error: empty input line.\n");
    return 0; 
    }

// CONVERT THE STRING TO A STATE
    nchars = read_state( str , &state );
    if (nchars <= 0) {
    printf("Error: invalid state entered.\n");
    return 0; 
    }

    printf("The state you entered is: ");
    print_state( stdout, &state );
    printf("\n");


//Create our openlist of nodes and add the start state to it. 
std::queue<Node*> openList;
std::deque<Node*> closedList;
openList.push(new Node(state));
while(!openList.empty()) {
    closedStateEncountered=false; 
    Node* currentNode=openList.front();
    if (is_goal(currentNode->getState())) {
        goalStateEncountered=true; 
        break;  
    }
for (std::deque<Node*>::iterator it=closedList.begin();it!=closedList.end();++it) {
        if (compare_states(it->getState(), currentNode->getState())) {
                //printf("repeat state encountered");
                closedStateEncountered=true;  
                break; 
        }               
    }


    //LOOP THROUGH THE CHILDREN ONE BY ONE
    if(closedStateEncountered) {
        openList.pop(); 
        continue; 
    }
    init_fwd_iter( &iter, &currentState );  // initialize the child iterator
    childCount=0; 
     while( ( ruleid = next_ruleid( &iter ) ) >= 0 ) {
        apply_fwd_rule( ruleid, &currentState, &child );
    //  print_state( stdout, &child );
        openList.push(child);
    //  printf("\n");
        childCount++; 
        }
    if (childCount>0) {
        nodeExpansions++;
    }   
    closedList.push_front(openList.front()); 
    openList.pop();
}

if (goalStateEncountered) {
    printf("Goal state encountered\nNodeExpansions: %d\n ", nodeExpansions);
    } else {
    printf("Goal state not found\n"); 
}

    return 0;
}

当我尝试调用我正在使用的api中定义如下的函数时,第82行出现了相关错误。 static int 比较状态(const state t *a,const state t *b);

具体导致错误的代码如下:

if (compare_states(it->getState(), currentNode->getState())) {

错误显示

error: request for member ‘getState’ in ‘* it.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator->()’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?) if (compare_states(it->getState(), currentNode->getState())) { ^ ./sliding_tile1x3.c:178:36: note: in definition of macro ‘compare_states’ #define compare_states(a,b) memcmp(a,b,sizeof(var_t)*NUMVARS)

如有任何帮助,我们将不胜感激。

这里:

for (std::deque<Node*>::iterator it=closedList.begin();it!=closedList.end();++it) {
    if (compare_states(it->getState(), currentNode->getState())) {

因为it被声明为

std::deque<Node*>::iterator it

它所指的是 Node*,而不是 Node,因此 it->foo 如果可以的话,会访问 Node* 的成员,而不是 Node* 的成员NodeNode 有一个成员函数 getStateNode* 没有,因此表达式

it->getState()

编译失败。这类似于如果 it 的类型为 Node** 则编译失败的方式。使用

(*it)->getState()

相反。 *it 是一个 Node*,它指向的东西有一个成员函数 getState,所以 thing->getState() 对它起作用。