没有找到Add_rect标识符

add_rect identifier not found c++

本文关键字:rect 标识符 Add      更新时间:2023-10-16

我得到一个错误在我的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_rectGraph类的内联成员函数,它需要与类的实例一起调用,因为它不是静态的。如果它是静态的,你需要用Graph::add_rect(...)

来调用它同时,你的Graph类是不是一个类模板,而你使用它,因为它是:Graph<int>

解决方案如下:

  1. add_rect移出您的类并将其定义为不属于Graph类的实用程序函数
  2. 首先创建Graph的实例,然后用.操作符调用它
  3. 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