没有找到Add_rect标识符
add_rect identifier not found c++
我得到一个错误在我的c++代码。
这里是相关代码的一部分,它位于pathfinding.h文件中。(GitHub链接到完整的项目在底部):模板struct Graph{//这将是映射定位;//基本上是一个(x,y)坐标
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相关。
下面是main中的代码:
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命名为墙在图形类。我加入了"寻径。h"。我尝试了前向声明,但这只会导致更多的错误。我不知道如何为unordered_set创建一个合适的哈希函数,有什么帮助吗?
还有,请告诉我如何使我的问题更好,第一次来这里。
Github链接https://github.com/Aopser101/pacman
感谢您的宝贵时间。
显示的代码中有多个错误:
add_rect(grid, 0, 0, 1, 10);
add_rect
是Graph
类的内联成员函数,它需要与类的实例一起调用,因为它不是静态的。如果它是静态的,你需要用Graph::add_rect(...)
Graph
类是不是一个类模板,而你使用它,因为它是:Graph<int>
。解决方案如下:
- 将
add_rect
移出您的类并将其定义为不属于Graph
类的实用程序函数 - 首先创建
Graph
的实例,然后用.
操作符调用它 - 将
Graph<int>
更改为Graph
或制作真正的类模板
您的add_rect
方法属于结构体,因此它应该在this
上操作,而不是在外部传递的对象上操作。它应该看起来像这样:
inline void add_rect(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) {
walls.insert(Graph::Location{ x, y });
}
}
}
应该这样调用:
Graph<int> grid(30, 10);
grid.add_rect(0, 0, 1, 10);
grid.add_rect(1, 0, 30, 1);
// etc
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- #ifdef和未声明的标识符
- f2、f3、f4标识符未找到
- 设置 Visual Studio for MPI: 找不到标识符错误
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 如何让 GCC/Clang 在保留标识符上出错
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 使用说明符 extern 声明的C++中的标识符链接
- C++:枚举:错误:应使用标识符而不是"}"
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- Visual Studio C++ PlaySound 标识符未定义
- 为什么我们不编写可以处理C++标识符的汇编器和链接器?
- 使用未声明的标识符"sqrt"?
- 找不到标识符合并
- 在模板化函数中重新定义(忽略)的 SWIG 标识符
- 在 c++ 中找不到此函数的标识符
- 对类 (C++) 中的私有变量使用未声明的标识符