Reputation: 2132
I am trying to iterate through a deque of objects of class Node and call the function get_State from each one. The code is as follows:
#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 encountered\nNodeExpansions: %d\n ", nodeExpansions);
} else {
printf("Goal state not found\n");
}
return 0;
}
the relevant error occurs on line 82 when I try to call the function that is defined as follows in the api I am using. static int compare states( const state t *a,const state t *b );
The exact code causing the error is as follows:
if (compare_states(it->getState(), currentNode->getState())) {
The error says
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)
Any assistance would be much appreciated.
Upvotes: 0
Views: 1946
Reputation: 44023
Here:
for (std::deque<Node*>::iterator it=closedList.begin();it!=closedList.end();++it) {
if (compare_states(it->getState(), currentNode->getState())) {
Because it
is declared as
std::deque<Node*>::iterator it
the thing to which it refers is a Node*
, not a Node
, so it->foo
would, if it could, access members of Node*
, not of Node
. Node
has a member function getState
, Node*
does not, and because of this the expression
it->getState()
fails to compile. This is analogous to the way it would fail to compile if it
were of type Node**
. Use
(*it)->getState()
instead. *it
is a Node*
, the thing it points to has a member function getState
, so thing->getState()
works for it.
Upvotes: 3