尝试迭代通过队列的对象指针在c++

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

本文关键字:对象 指针 c++ 队列 迭代      更新时间:2023-10-16

我试图通过类Node的对象的deque进行迭代,并从每个对象调用函数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 encounterednNodeExpansions: %dn ", nodeExpansions);
    } else {
    printf("Goal state not foundn"); 
}
    return 0;
}

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

导致错误的确切代码如下:

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

错误提示

错误:在' * '中请求成员' getState '.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator->() ',它是指针类型' Node* '(也许你打算使用` -> ` ?) if (compare_states(it->getState());currentNode -> getState ())) {^ ./sliding_tile1x3.c:178:36:注:在定义宏' 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的成员。Node有成员函数getState, Node*没有,因此表达式

it->getState()

编译失败。这类似于如果itNode**类型,则编译失败的方式。使用

(*it)->getState()

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