繁体   English   中英

C ++在Eclipse CDT中编译我的代码它运行正常,但是从命令行执行此操作会导致大量错误

[英]C++ Compiling my code in Eclipse CDT it works fine, but doing so from the command line causes a huge amount of errors

我正在使用Eclpise CDT在C ++中开发A *搜索我的搜索工作并将预期结果打印到Eclispe中的终端。

我遇到的问题是,如果我尝试从命令行(Ubuntu 13.10)编译/运行,我会收到相当大量的编译错误,它会在程序中抱怨所有函数或参数。

代码我将发布所有以防任何人想要尝试并模拟此错误

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());

    }
};

从Eclpise打印代码:

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

在我尝试编译的目录中( using ls ):

Debug  Main.cpp  main.h  main.h.gch  Node.cpp  Node.h  Node.h.gch

编译错误是( 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);
                                                  ^

构建C ++项目通常分两步完成:将所有.cpp文件编译为.o文件,然后将它们链接到一个可执行文件中。 头文件(.h)不是自己编译的,只是包含在.cpp文件中。

如果您正在使用Eclipse,则不需要知道这一点,因为它会自动透明地处理这两个步骤,如您所见。

但是,如果您是从命令行构建的,则您必须自己执行这些步骤,或者使用像Make这样的工具。

要尝试Make,请将以下内容放在名为Makefile ,然后从命令行运行make -B

astarcpp: Main.o Node.o
Main.o: Main.cpp
Node.o: Node.cpp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM