# Pathfinding Algorithm Error on Sketch

This is my arduino code.
I am trying to do pathfinding

But my problem is that. I got an error saying this:

algorithm:4: error: expected ‘,’ or ‘…’ before ‘&’ token
algorithm:4: error: ISO C++ forbids declaration of ‘Location’ with no type
algorithm.ino: In function
‘String pathFind(const Location&, const Location&)’:
algorithm:107: error: expected initializer before ‘<’ token
algorithm:128: error: ‘q’ was not declared in this scope

But this is written:

const int IDIM = 5; // horizontal size of the squares
const int JDIM = 5; // vertical size size of the squares
const int NDIR = 4; // number of possible directions to go at any position

// if NDIR = 4
const int iDir[NDIR] = {1, 0, -1, 0};
const int jDir[NDIR] = {0, 1, 0, -1};

// if NDIR = 8
//const int iDir[NDIR] = {1, 1, 0, -1, -1, -1, 0, 1};
//const int jDir[NDIR] = {0, 1, 1, 1, 0, -1, -1, -1};

int squares[IDIM][JDIM];

// list of closed (check-out) nodes
int closedNodes[IDIM][JDIM];

// list of open (not-yet-checked-out) nodes
int openNodes[IDIM][JDIM];

// map of directions (0: East, 1: North, 2: West, 3: South)
int dirMap[IDIM][JDIM];

struct Location
{
int row, col;

Location()
{
row = col = 0;
};

Location(int r, int c)
{
row = r;
col = c;
};
};

class Node
{
// current position
int rPos;
int cPos;

// total distance already travelled to reach the node
int GValue;

// FValue = GValue + remaining distance estimate
int FValue; // smaller FValue gets priority

public:
Node(const Location &loc, int g, int f)
{rPos = loc.row; cPos = loc.col; GValue = g; FValue = f;}

Location getLocation() const {return Location(rPos,cPos);}
int getGValue() const {return GValue;}
int getFValue() const {return FValue;}

void calculateFValue(const Location& locDest)
{
FValue = GValue + getHValue(locDest) * 10;
// cout<<"F Value: "<<FValue<<endl;
}

void updateGValue(const int & i) // i: direction
{
GValue += (NDIR == 4 ? (i % 2 == 0 ? 10 : 14) : 10);
// cout<<"G Value: "<<GValue<<endl;
}

// Estimation function for the remaining distance to the goal.
const int & getHValue(const Location& locDest) const
{
static int rd, cd, d;
rd = locDest.row - rPos;
cd = locDest.col - cPos;

// Euclidian Distance
// d = static_cast(sqrt((double)(rdrd+cdcd)));

// Manhattan distance
d = abs(rd) + abs(cd);

// Chebyshev distance
//d = max(abs(rd), abs(cd));

return(d);
}

// Determine FValue (in the priority queue)
friend bool operator<(const Node & a, const Node & b)
{
return a.getFValue() > b.getFValue();
}

};

// A-star algorithm.
// The path returned is a string of direction digits.
String pathFind( const Location &locStart , const Location &locFinish )

{
// list of open (not-yet-checked-out) nodes
static priority_queue q[2];

// q index
static int qi;
static Node* pNode1;
static Node* pNode2;
static int i, j, row, col, iNext, jNext;
static char c;
qi = 0;
/**
// reset the Node lists (0 = “.”)
for(j = 0; j < JDIM; j++) {
for(i = 0; i < IDIM; i++) {
closedNodes*[j] = 0;*
_ openNodes*[j] = 0;_
_
}_
_ }/
* // create the start node and push into list of open nodes*
* pNode1 = new Node(locStart, 0, 0);*
* pNode1->calculateFValue(locFinish);*
q[qi].push(*pNode1);_

_ // A* search_
* while(!q[qi].empty()) {*
* // get the current node w/ the lowest FValue*
* // from the list of open nodes*
* pNode1 = new Node( q[qi].top().getLocation(),*
* q[qi].top().getGValue(), q[qi].top().getFValue());*
* row = (pNode1->getLocation()).row;*
* col = pNode1->getLocation().col;*
* //cout << “row, col=” << row << “,” << col << endl;*
* // remove the node from the open list*
* q[qi].pop();*
* openNodes[row][col] = 0;*
* // mark it on the closed nodes list*
* closedNodes[row][col] = 1;*
* // stop searching when the goal state is reached*
* if(row == locFinish.row && col == locFinish.col) {*

* // drawing direction map*
* //cout << endl;*
* for(j = JDIM - 1; j >= 0; j–) {*
* for(i = 0; i < IDIM; i++) {*
_ //cout << dirMap*[j];
}
//cout << endl;
}
//cout << endl;
// generate the path from finish to start from dirMap*
* String path = “”;
while(!(row == locStart.row && col == locStart.col)) {
j = dirMap[row][col];*_

* c = ‘0’ + (j + NDIR) % NDIR;*
* path = c + path;*
* // cout<<"path: "<<path<<endl;*
* row += iDir[j];*
* col += jDir[j];*

* }*
* // garbage collection*
* delete pNode1;*
* // empty the leftover nodes*
* while(!q[qi].empty()) q[qi].pop(); *
* return path;*
* }*
* // generate moves in all possible directions*
* for(i = 0; i < NDIR; i++) {*
_ iNext = row + iDir*;
jNext = col + jDir;
// if not wall (obstacle) nor in the closed list*

* if(!(iNext < 0 || iNext > IDIM - 1 || jNext < 0 || jNext > JDIM - 1 ||
squares[iNext][jNext] == 1 || closedNodes[iNext][jNext] == 1)) {*_

* // generate a child node*
* pNode2 = new Node( Location(iNext, jNext), pNode1->getGValue(), pNode1->getFValue());*
* pNode2->updateGValue(i);*
* pNode2->calculateFValue(locFinish);*
* // if it is not in the open list then add into that*
* if(openNodes[iNext][jNext] == 0) {*
* openNodes[iNext][jNext] = pNode2->getFValue();*
_ q[qi].push(pNode2);
// mark its parent node direction*

* dirMap[iNext][jNext] = (i + NDIR/2) % NDIR;*_

* }*

// already in the open list
* else if(openNodes[iNext][jNext] > pNode2->getFValue()) {*
* // update the FValue info*
* openNodes[iNext][jNext] = pNode2->getFValue();*
* // update the parent direction info, mark its parent node direction*
* dirMap[iNext][jNext] = (i + NDIR/2) % NDIR;*
* // replace the node by emptying one q to the other one*
* // except the node to be replaced will be ignored*
* // and the new node will be pushed in instead*
* while(!(q[qi].top().getLocation().row == iNext &&*
* q[qi].top().getLocation().col == jNext)) { *
* q[1 - qi].push(q[qi].top());*
* q[qi].pop(); *
* }*
* // remove the wanted node*
* q[qi].pop();*

* // empty the larger size q to the smaller one*
* if(q[qi].size() > q[1 - qi].size()) qi = 1 - qi;*
* while(!q[qi].empty()) { *
* q[1 - qi].push(q[qi].top());*
* q[qi].pop(); *
* }*
* qi = 1 - qi;*
_ q[qi].push(pNode2);
}
else delete pNode2;
}
}
delete pNode1;
}
// no path found*

* return “”;
}*

Could you please tell me guys what is the problem of my Code that is not applicable on sketch.
Any suggestions how to fix this errors? Thanks Guys…_

After 11 posts, you should have already read Nick Gammon’s two posts at the top of this Forum. Reading those and following his suggestions, especially on the use of code tags, will make it easier for us to help you.

Please use code tags, otherwise code gets mangled which helps no-one. With code tags its possible to copy and paste painlessly and start looked at an issue - don't bother and most of us won't bother...