2016-12-01 70 views
-3

我目前正在C++中實現A *尋路算法。我嘗試運行我的代碼來查看顯示網格函數是否正常工作,但是得到了C2678錯誤:二進制'<':沒有發現操作符需要'const Coord'類型的左側操作數(或者沒有可接受的轉換) 。錯誤C2678 C++ A *尋路

我知道我的程序很混亂,可能效率不高,但我試圖在優化之前得到一個基本版本。是因爲我試圖輸出一個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; 
} 
+2

請編輯您的問題以提供[mcve]。 –

+0

我沒有在您提供的代碼中看到operator <的實現。 – drescherjm

+3

['std :: set'](http://www.cplusplus.com/reference/set/set/)需要具有嚴格弱排序*的元素。你'Coord'結構不提供任何排序​​操作(沒有'std :: less'的專門化,沒有'operator <()')。因此'std :: set'不知道如何比較你的'Coord'對象和咕嚕聲。 – dhke

回答

0

解決問題與std::set要求嚴格弱排序和你Coord類最簡單的方法是提供operator <比較在Coordxy值,並返回是否一個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::tiex成分比較這樣做,那麼如果相等,比較y組件。返回比較結果(如果第一組x,y組分小於第二組x,y組分,則返回true,否則返回false)。

Live Example here

+0

感謝您的幫助保羅,現在按預期工作。這是我第一次處理嚴格的弱點排序,所以我不知道發生了什麼。欣賞它! –