gpt4 book ai didi

C++ 在 Eclipse CDT 中编译我的代码它工作正常,但是从命令行这样做会导致大量错误

转载 作者:行者123 更新时间:2023-11-28 02:54:08 29 4
gpt4 key购买 nike

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

我遇到的问题是,如果我尝试从命令行 (Ubuntu 13.10) 编译/运行,我会收到相当多的编译错误,这些错误几乎会提示程序中的每个函数或参数。

代码我会把它全部贴出来,以防有人想尝试模拟这个错误:

主要.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;
}

}

节点.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);
}

主要.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]);

节点.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

在我尝试编译的目录中(使用 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

关于C++ 在 Eclipse CDT 中编译我的代码它工作正常,但是从命令行这样做会导致大量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392540/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com