Reputation: 1442
I am using the Eclpise CDT to develop an A* search in C++ i have the search working and it prints the expected result to the terminal in Eclispe.
The issue i am having is if i try and compile/run from the command line (Ubuntu 13.10) i get a rather large amount of compilation errors, which complain about pretty muchc every function or parameter in the program.
code i will post it all in case anybody wants to try and emulate this error:
main.cpp
#include <iostream>
#include <ostream>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <list>
#include "Node.h"
#include "main.h"
int main(){
int map[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,2},
{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0},
{0,0,3,0,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}};
using namespace std;
list<Node*> openList;
vector<Node*> closedList;
Node *end;
Node *start = initiateStart(map);
openList.push_front(start);
cout <<"Start index: x " << start->getX() << " y " <<start->getY() << endl;
while (!openList.empty()) {
Node *best = openList.front();
openList.pop_front();
if(!checkInClosedList(closedList, best->getX(), best->getY())){
calcManhattanDistance(best, map);
if(best->getValue() == 3){
end = best;
cout <<"end index: x " << end->getX() << " y " <<end->getY() << endl;
checkPath(end, map);
for (std::list<Node*>::iterator iter = openList.begin(); iter != openList.end(); ++iter) {
(*iter)-> ~Node();
}
exit(1);
}
if(map[best->getX()][best->getY()-1] != 1 && best->getY() - 1 > -1){
if(placeInOpen(openList,best->getX(), best->getY() - 1)){
openList.push_front(generateLeftChild(best, map));
}
}
//to the right
if(map[best->getX()][best->getY()+1] != 1 && best->getY() + 1 < 20){
if(placeInOpen(openList,best->getX(), best->getY() + 1)){
openList.push_front(generateRightChild(best, map));
}
}
//above
if(map[best->getX()-1][best->getY()] != 1 && best->getX() - 1 > -1){
if(placeInOpen(openList,best->getX()-1, best->getY())){
openList.push_front(generateAboveChild(best, map));
}
}
//below
if(map[best->getX()+1][best->getY()] != 1 && best->getX() + 1 < 20){
if(placeInOpen(openList,best->getX()+1, best->getY())){
openList.push_front(generateBelowChild(best, map));
}
}
closedList.push_back(best);
}
openList.sort(NodeComparator());
}
return 0;
}
Node* initiateStart(int m[20][20]){
Node *start;
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
if(m[i][j] == 2){
start = new Node(i, j, m[i][j], 0, NULL);
}
}
}
return start;
}
Node* generateLeftChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX(), parent->getY() - 1, m[parent->getX()][parent->getY() - 1],
parent->getGCost() + 1, parent);
calcManhattanDistance(child, m);
return child;
}
Node* generateRightChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX() , parent->getY() + 1, m[parent->getX()][parent->getY() + 1],
parent->getGCost() + 1, parent);
calcManhattanDistance(child, m);
return child;
}
Node* generateAboveChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX() - 1, parent->getY(), m[parent->getX() - 1][parent->getY()],
parent->getGCost() + 1, parent);
calcManhattanDistance(child, m);
return child;
}
Node* generateBelowChild(Node *parent, int m[20][20]){
Node *child;
child = new Node(parent->getX() + 1, parent->getY(), m[parent->getX() + 1][parent->getY()],
parent->getGCost() + 1, parent);
calcManhattanDistance(child, m);
return child;
}
void calcManhattanDistance(Node *node, int m[20][20]){
int tempX;
int tempY;
double manhattanDistance;
int differenceX;
int differenceY;
//std::cout << "node x: " << node->getX() << " node y: " << node->getY() << std::endl;
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
if(m[i][j] == 3){
tempX = i;
tempY = j;
}
}
}
//sum of term difference, none of these can be negative hense the std::abs
differenceX = tempX - node->getX();
differenceY = tempY - node->getY();
manhattanDistance = std::abs(differenceX) + std::abs(differenceY);
//std::cout << "Manhattan distance: " << manhattanDistance << std::endl;
node->setHCost(manhattanDistance);
}
bool checkInClosedList(std::vector<Node*>& v,int x, int y){
for (std::vector<Node*>::iterator iter = v.begin(); iter != v.end(); ++iter) {
if((*iter)->getX() == x && (*iter)->getY() == y){
return true;
}
}
return false;
}
bool placeInOpen(std::list<Node*>& v,int x, int y){
for (std::list<Node*>::iterator iter = v.begin(); iter != v.end(); ++iter) {
if((*iter)->getX() == x && (*iter)->getY() == y){
return false;
}
}
return true;
}
void checkPath(Node *end, int m[20][20]){
int tempX, tempY;
Node *temp = end;
while(temp != NULL){
tempX = temp->getX();
tempY = temp->getY();
std:: cout << tempX << " " << tempY << std::endl;
m[tempX][tempY] = 4;
temp = temp->getParent();
}
printMap(m);
}
void printMap(int m[20][20]){
std::cout << "printy mcprint" << std::endl;
for(int i = 0; i< 20; i++){
for(int j = 0; j< 20; j++){
std::cout << m[i][j];
}
std::cout<<std::endl;
}
}
node.cpp
#include <stdlib.h>
#include "Node.h"
Node::Node(){
this->xCoord = 0;
this->yCoord = 0;
this->value = 0;
this->parent = NULL;
this->fCost = 0;
this->gCost = 0;
this->hCost = 0.0;
}
Node::Node(int _x, int _y, int _value, int cost, Node* parent){
this->xCoord = _x;
this->yCoord = _y;
this->value = _value;
this->gCost = cost;
this->parent = parent;
this->hCost = 0.0;
this->fCost = 0;
}
void Node::setParent(Node* par){
this->parent = par;
}
int Node::getX(){
return xCoord;
}
int Node::getY(){
return yCoord;
}
int Node::getValue(){
return value;
}
double Node::getGCost(){
return gCost;
}
double Node::getFCost(){
return gCost + hCost;
}
double Node::getHCost(){
return hCost;
}
Node* Node::getParent(){
return parent;
}
void Node::setHCost(double cost){
this->hCost = cost;
}
Node::~Node(){
delete(this);
}
main.h
#include "Node.h"
Node* initiateStart(int m[20][20]);
Node* generateLeftChild(Node* parent, int m[20][20]);
Node* generateRightChild(Node* parent, int m[20][20]);
Node* generateAboveChild(Node* parent, int m[20][20]);
Node* generateBelowChild(Node* parent, int m[20][20]);
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
bool placeInOpen(std::list<Node*>& v,int x, int y);
void checkPath(Node *end, int m[20][20]);
void calcManhattanDistance(Node *node, int m[20][20]);
void printMap(int m[20][20]);
node.h
class Node{
private:
int xCoord;
int yCoord;
int value;
double fCost;
double gCost;
double hCost;
Node* parent;
public:
Node();
Node(int x, int y, int value, int cost, Node* parent);
void setParent(Node* parent);
int getX();
int getY();
int getValue();
double getHCost();
double getFCost();
double getGCost();
Node* getParent();
void setHCost(double hCost);
~Node();
};
struct NodeComparator {
bool operator()(Node* first, Node* second) {
return (first->getFCost() < second->getFCost());
}
};
Running the code from Eclpise prints:
Start index: x 0 y 19
end index: x 9 y 2
9 2
9 3
9 4
8 4
7 4
6 4
5 4
4 4
3 4
2 4
2 5
2 6
2 7
2 8
3 8
4 8
5 8
5 9
5 10
5 11
5 12
5 13
5 14
5 15
5 16
5 17
5 18
5 19
4 19
3 19
2 19
1 19
0 19
printy mcprint
00000000000111000004
00000000000111000004
00004444400111000004
00004111400010000004
00004111400000000004
00004111444444444444
00004111110000001000
00004111110000001000
00004111110000001000
00444111111100001000
00000111111000011000
00000111110000000000
00000111110000000000
00011111110000000000
00011111110001000000
00011111110001000000
00000000000001000000
00000000000001000000
00000000000001000000
00000000000001000000
in the directory where i try to compile (using ls
):
Debug Main.cpp main.h main.h.gch Node.cpp Node.h Node.h.gch
the compilation errors are (using g++ -o astarcpp *.cpp *.h
)
main.h:18:24: error: ‘vector’ is not a member of ‘std’
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:18:40: error: expected primary-expression before ‘*’ token
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:18:41: error: expected primary-expression before ‘>’ token
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:18:44: error: ‘v’ was not declared in this scope
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:18:47: error: expected primary-expression before ‘int’
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:18:54: error: expected primary-expression before ‘int’
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:18:59: error: expression list treated as compound expression in initializer [-fpermissive]
bool checkInClosedList(std::vector<Node*>& v, int x, int y);
^
main.h:19:18: error: ‘list’ is not a member of ‘std’
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
main.h:19:32: error: expected primary-expression before ‘*’ token
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
main.h:19:33: error: expected primary-expression before ‘>’ token
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
main.h:19:36: error: ‘v’ was not declared in this scope
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
main.h:19:38: error: expected primary-expression before ‘int’
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
main.h:19:45: error: expected primary-expression before ‘int’
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
main.h:19:50: error: expression list treated as compound expression in initializer [-fpermissive]
bool placeInOpen(std::list<Node*>& v,int x, int y);
^
Upvotes: 0
Views: 101
Reputation: 19695
Building a C++ project is usually done in two steps: compiling all the .cpp files into .o files, then linking them together into an executable. The header files (.h) are not compiled on their own, just included from the .cpp files.
You don't need to know this if you're using Eclipse, as it handles both steps for you automatically transparently, as you've seen.
If you're building from the command line, however, you'll either have to do the steps yourself or have a tool like Make do them for you.
To try Make, put the following in a file called Makefile
, then run make -B
from the command line:
astarcpp: Main.o Node.o
Main.o: Main.cpp
Node.o: Node.cpp
Upvotes: 2