我在我的C++代碼中收到錯誤。找不到添加標識符C++
這裏是相關代碼的一部分,它位於pathfinding.h文件中。 (GitHub鏈接到完整的項目是在底部): template struct Graph {//這將是地圖 typedef元組位置; //基本上是一個(x,y)座標 static array DIRS;
unordered_set<Location>walls;
int width, height;
Graph(int width_, int height_) :width(width_), height(height_){} //constructor
inline bool in_bounds(Location id){ //make sure in bounds
int x, y;
tie(x, y) = id;
return 0 <= x<width && 0 <= y<height;
}
inline bool passable(Location id){ //check if wall
return !walls.count(id);
}
vector<Location> neighbors(Location id){ //return 4 neighbors
int x, y, dx, dy;
tie(x, y) = id;
bool possible;
vector<Location> results;
for (auto dir : DIRS) {
tie(dx, dy) = dir;
Location next(x + dx, y + dy);
if (in_bounds(next) && passable(next)) {
results.push_back(next);
}
}
if ((x + y) % 2 == 0) {
// aesthetic improvement on square grids
std::reverse(results.begin(), results.end());
}
return results;
}
inline void add_rect(Graph& grid, int x1, int y1, int x2, int y2) { //create walls (this part doesn't work atm)
for (int x = x1; x < x2; ++x) {
for (int y = y1; y < y2; ++y) {
grid.walls.insert(Graph::Location{ x, y });
}
}
}
};
其實它主要是相關的add_rect函數和unordered_set。
這裏是主代碼:
Graph<int> grid(30, 10); //all
add_rect(grid, 0, 0, 1, 10); //of
add_rect(grid, 1, 0, 30, 1);
add_rect(grid, 12, 1, 13, 2);
add_rect(grid, 16, 1, 17, 2);
add_rect(grid, 12, 2, 13, 3);
add_rect(grid, 16, 2, 17, 3); //this
add_rect(grid, 12, 3, 17, 4); //doesn't
add_rect(grid, 1, 9, 30, 10); //work
add_rect(grid, 29, 1, 30, 9); //right
add_rect(grid, 4, 5, 25, 6); // now
這是錯誤的清單:
error C3861: 'add_rect': identifier not found (all of the lines with add_rect in the main)
error C2338: The C++ Standard doesn't provide a hash for this type.
基本上add_rect應該在創建牆,並將它們添加到unordered_set命名的牆壁Graph類。我已經包含了「pathfinding.h」。我嘗試了前向聲明,但它只是導致更多的錯誤。我不知道如何爲unordered_set創建合適的哈希函數,對此有何幫助?
另外,請告訴我如何更好地提出我的問題,這裏的第一個計時器。
Github上鍊接:https://github.com/Aopser101/pacman
感謝您的時間。
移動你的'的add_rect'外'Graph' – torvin
試過了,它給了一堆新的錯誤。 –
查看我的回答 – torvin