尝试迭代通过队列的对象指针在c++
Trying to iterate through queue of object pointers in c++
我试图通过类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, ¤tState ); // initialize the child iterator
childCount=0;
while( ( ruleid = next_ruleid( &iter ) ) >= 0 ) {
apply_fwd_rule( ruleid, ¤tState, &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()
编译失败。这类似于如果it
是Node**
类型,则编译失败的方式。使用
(*it)->getState()
。*it
是一个Node*
,它指向的东西有一个成员函数getState
,所以thing->getState()
适用于它。
相关文章:
- 对象指针在c++中是如何工作的
- C++ 对象指针数组的复制构造函数
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 正确初始化和销毁对象指针的C++数组?
- 如何深度复制链表对象指针
- 对象指针 c++ 的全局向量错误
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 静态对象指针
- 正在将对象指针数组初始化为NULL
- 如何使用条件表达式返回对象指针?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- 使用C对象指针构建PyObject*
- 如何在使用对象指针时访问成员函数
- 静态强制转换允许转换对象指针,但不允许转换整数
- C++ abort() 在函数内的抽象类对象指针调用上
- 指向函数的对象指针
- 访问指向对象指针向量的指针的第一个元素?
- 如何将对象/指针正确存储到 Qlist 中
- 对象指针打印结果以相反的顺序进行