【问题标题】:Error C2678 C++ A* Pathfinding错误 C2678 C++ A* 寻路
【发布时间】:2016-12-01 17:57:26
【问题描述】:

我目前正致力于在 C++ 中实现 A* 寻路算法。我试图运行我的代码以查看显示网格函数是否正常工作,但出现 C2678 错误:二进制“

我知道我的程序很混乱而且可能根本没有效率,但是我试图在优化之前让一个基本版本工作。错误是因为我试图输出 Coord 结构的布尔值吗?

代码:

#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <vector>
#include <set>

using std::chrono::milliseconds;
using std::chrono::duration_cast;
using std::this_thread::sleep_for;

typedef std::chrono::steady_clock the_clock;

struct Location {
    int g = 0; // Distance covered so far 
    int h = 0; // Estimate of distance to goal
    float f = 0; // Estimated cost of the complete path
    bool walkable = 0; // 0 = Walkable, 1 = Wall
};

// Structure 
struct Coord {
    int x;
    int y;
    Location location;
};

// Declare size of grid
#define WIDTH 10
#define HEIGHT 10

typedef Location Array[HEIGHT][WIDTH];
Location grid[HEIGHT][WIDTH]; // Create an array of locations

void displayGrid() {
    /* Displays the Grid to the console! */
    system("CLS");
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            std::cout << grid[y][x].walkable;
        }
        std::cout << "\n";
    }
    sleep_for(milliseconds(100)); // Visual delay
}

void initialiseGrid() {
    /* Fills the Grid array with values */
    srand((unsigned)time(0));

    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            grid[y][x].walkable = 0; 
    }
}

/* Test grid */
grid[4][2].walkable = 1;
grid[5][2].walkable = 1;
grid[4][3].walkable = 1;
grid[5][3].walkable = 1;
grid[4][5].walkable = 1;
grid[5][5].walkable = 1;
grid[4][6].walkable = 1;
grid[5][6].walkable = 1;
}

void Astar(Coord startPoint, Coord endPoint) {
    /**/
    std::set<Coord> closedSet = {}; // Nodes that do not have to be considered again
    std::set<Coord> openSet = {}; // Nodes still to be considered to find the shortest path

    Coord currentNode; // Current node
    currentNode.x = startPoint.x;
    currentNode.y = startPoint.y;
    currentNode.location.g = 0; // 0 Distance from starting point

    openSet.insert(currentNode); // Insert starting node

    while (openSet.empty() == false) { // Loop while open list is not empty

        for (std::set<Coord>::iterator it = openSet.begin(); it != openSet.end(); it++) { // Iterate through each element in the open set to find the lowest F value
            if ((*it).location.f < currentNode.location.f) { // Check if iterator f value is smaller than the current value
                currentNode = *it; // Update the current node
            }
        }

        openSet.erase(currentNode); // Drop from the open set since been checked
        closedSet.insert(currentNode); // Add to the closed set
    }
}


int main(int argc, char *argv[]) {
    // Set start and end points
    Coord start;
    start.x = 3;
    start.y = 3;
    Coord end;
    end.x = 5;
    end.y = 6;

    initialiseGrid(); // Put -1 (empty) in

    // Start timing
    the_clock::time_point startTime = the_clock::now();

    // Stop timing
    the_clock::time_point endTime = the_clock::now();

    // Compute the difference between the two times in milliseconds
    auto time_taken = duration_cast<milliseconds>(endTime - startTime).count();

    displayGrid();

    std::cout << "That took: " << time_taken << " ms" << std::endl;

    return 0;
}

【问题讨论】:

  • edit您的问题提供minimal reproducible example
  • 我在你提供的代码中没有看到 operator
  • std::set 要求元素具有严格的弱排序。您的Coord 结构不提供任何排序​​操作(std::less 没有特化,没有operator&lt;())。因此std::set 不知道如何比较您的Coord 对象和呱呱声。
  • 错误是因为我试图输出 Coord 结构的布尔值吗? 不是。错误是因为您使用的是 @ 987654331@。 @dhke 很好地解释了这一点。
  • 所以我应该创建一个排序函数或重载结构内的

标签: c++ a-star


【解决方案1】:

解决std::set 需要严格弱排序问题和Coord 类的问题的最简单方法是提供一个operator &lt; 比较xy 中的Coord 值,以及使用这些值返回一个Coord 是否小于另一个Coord

您可以通过std::tie 做到这一点

#include <tuple>
//...
struct Coord {
    int x;
    int y;
    Location location;
    bool operator <(const Coord& c) const 

    // returns true if this->x and this->y < c.x and c.y, false otherwise
    { return std::tie(x,y) < std::tie(c.x,c.y); }  
};

std::tie 比较 x 组件,然后如果相等,则比较 y 组件。返回比较的结果(如果第一组 x,y 分量小于第二组 x,y 分量,则返回 true,否则返回 false)。

Live Example here

【讨论】:

  • 感谢您的帮助 Paul,它现在按预期工作。这是我第一次处理严格弱排序,所以我不知道发生了什么。欣赏!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多