c++ - 显式调用之前的表达式必须具有指向func类型的指针

标签 c++

我有一个要使用的类,但在main函数ёstartё中,执行以下错误expression preceeding of apparent call must have pointer-to func type时未执行

#include <queue>
#include <limits>
#include <cmath>
#include <iostream>
// represents a single pixel
class Node {
public:
    int idx;     // index in the flattened grid
    float cost;  // cost of traversing this pixel

    Node(int i, float c) : idx(i), cost(c) {}
};


bool operator<(const Node &n1, const Node &n2) {
    return n1.cost > n2.cost;
}

bool operator==(const Node &n1, const Node &n2) {
    return n1.idx == n2.idx;
}

// various grid heuristics:
// http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#S7
float linf_norm(int i0, int j0, int i1, int j1) {
    return std::max(std::abs(i0 - i1), std::abs(j0 - j1));
}

// manhattan distance
float l1_norm(int i0, int j0, int i1, int j1) {
    return std::abs(i0 - i1) + std::abs(j0 - j1);
}

// weights:        flattened h x w grid of costs
// h, w:           height and width of grid
// start, goal:    index of start/goal in flattened grid
// diag_ok:        if true, allows diagonal moves (8-conn.)
// paths (output): for each node, stores previous node in path

class Astar {
public:
    float* weights = nullptr;
    int h;
    int w;
    int start;
    int goal;
    bool diag_ok;
    int* paths = nullptr;

    void setVariables(float* weightsInput,  int hInput,  int wInput,  int startInput,  int goalInput, bool diag_okInput, int* pathsInput) {
        weights = weightsInput;
        h = hInput;
        w = wInput;
        start = startInput;
        goal = goalInput;
        diag_ok = diag_okInput;
        paths = pathsInput;

    }

    void start() {
        const float INF = std::numeric_limits<float>::infinity();

        std::cout << "width : " << w << " " << "height : " << h << std::endl;
        std::cout << "start : " << start << " goal : " << goal << std::endl;

        Node start_node(start, 0.);
        Node goal_node(goal, 0.);

        float* costs = new float[h * w];
        for (int i = 0; i < h * w; ++i)
            costs[i] = INF;
        costs[start] = 0.;

        std::priority_queue<Node> nodes_to_visit;
        nodes_to_visit.push(start_node);

        int* nbrs = new int[3];

        bool solution_found = false;
        while (!nodes_to_visit.empty()) {
            // .top() doesn't actually remove the node
            Node cur = nodes_to_visit.top();

            if (cur == goal_node) {
                solution_found = true;
                break;
            }

            nodes_to_visit.pop();

            int row = cur.idx / w;
            int col = cur.idx % w;
            bool allowDiag;
            // check bounds and find up to eight neighbors: top to bottom, left to right
            // can move only right\down\down - right so we can max have 3 neighbours

            nbrs[0] = (col + 1 < w) ? cur.idx + 1 : -1; // right
            nbrs[1] = (row + 1 < h) ? cur.idx + w : -1; // down
            allowDiag = (weights[cur.idx + w + 1] == 14) ? true : false;
            nbrs[2] = (allowDiag) ? cur.idx + w + 1 : -1; // down-right
            std::cout << "right-bottom node :  " << weights[cur.idx + w + 1] << std::endl;
            float heuristic_cost;
            for (int i = 0; i < 3; ++i) {
                std::cout << "neighbours : " << nbrs[i] << " ";
                if (nbrs[i] >= 0) {
                    // the sum of the cost so far and the cost of this move
                    float new_cost = costs[cur.idx] + weights[nbrs[i]];
                    if (new_cost < costs[nbrs[i]]) {
                        // estimate the cost to the goal based on legal moves
                        if (allowDiag) {
                            heuristic_cost = linf_norm(nbrs[i] / w, nbrs[i] % w,
                                goal / w, goal    % w);
                        }
                        else {
                            heuristic_cost = l1_norm(nbrs[i] / w, nbrs[i] % w,
                                goal / w, goal    % w);
                        }

                        // paths with lower expected cost are explored first
                        float priority = new_cost + heuristic_cost;
                        nodes_to_visit.push(Node(nbrs[i], priority));

                        costs[nbrs[i]] = new_cost;
                        paths[nbrs[i]] = cur.idx;
                    }
                }
            }
            std::cout << "\n";
        }

        delete[] costs;
        delete[] nbrs;

        //return solution_found;
    }
};


int main() {
    Astar astarPathfinding;

    float* weights;
    int h;
    int w;
    int start;
    int goal;
    bool diag_ok;
    int* paths;

    astarPathfinding.setVariables(weights, h, w, start, goal, diag_ok, paths);
    astarPathfinding.start(); // error

    return 0;
}

最佳答案

您具有“开始”作为成员,具有“开始”作为功能。
重命名其中之一将解决您的错误。

关于c++ - 显式调用之前的表达式必须具有指向func类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62633916/

相关文章:

c++ - 顶点着色器的定点算法

java - 为什么 java 不像 C++ 那样记录渐近复杂性?

C++ multimap 导致程序退出缓慢?

c++ - 如何将模板作为模板参数传递给模板?

c++ - boolean 逻辑问题

c++ - 基于范围的for循环可以在一个范围内工作吗

c++ - 替换 av_read_frame() 以减少延迟

c++ - 如何在 C++ 中使用 MXNet 模型?

c++ - 少量项目的 HashMap

c++ - 扩展名为 ".a"的共享库?