gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:15:36 24 4
gpt4 key购买 nike

我正在尝试遍历 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())) {

因为被声明为

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

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

it->getState()

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

(*it)->getState()

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

关于c++ - 尝试遍历 C++ 中的对象指针队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30540560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com