嵌套类:如何访问封闭类中的成员变量

机器人AI

以下代码段旨在将所有内容存储pointsmainFunc包围类的成员函数Solution中的priority_queue(即pq)中,从而使所有内容points按照它们到的距离排序origin但是,编译器报告错误:

error: invalid use of non-static data member 'Solution::ori'

然后我的3号线改Point oristatic Point ori和变革ori,以Solution::ori在功能distance(Point p)时,会出现链接错误:

undefined reference to 'Solution::ori'

有人可以帮我吗?提前致谢!

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */

class Solution {
private:
    Point ori;
    class Comparator {
    public:
        // calculate the euclidean distance between p and ori
        int distance(Point p) {
            return pow(p.x-ori.x, 2) + pow(p.y-ori.y, 2);
        }
        // overload the comparator (), the nearest point to 
        // origin comes to the first
        bool operator() (Point l, Point r) {
            if (distance(l) > distance(r)) {
                return true;
            }
        }        
    };

public:
    /*
     * @param points: a list of points
     * @param origin: a point
     */
    void mainFunc(vector<Point> points, Point origin) {
        ori = origin;
        priority_queue<Point, vector<Point>, Comparator> pq;
        for (int i = 0; i < points.size(); i++) {
            pq.push(points[i]);
        }
    }
};
用户名

您可以修改Comparator声明以Point在其构造函数中采用某个值:

class Solution {
private:
    Point ori;
    class Comparator {
    public:
        // Save the value in the functor class
        Comparator(const Point& origin) : origin_(origin) {}

        // calculate the euclidean distance between p and origin
        int distance(Point p) {
            return pow(p.x-origin_.x, 2) + pow(p.y-origin_.y, 2);
        }
        // overload the comparator (), the nearest point to 
        // origin comes to the first
        bool operator() (Point l, Point r) {
            if (distance(l) > distance(r)) {
                return true;
            }
        }        
    private:
         Point origin_;
    };

public:
    /*
     * @param points: a list of points
     * @param origin: a point
     */
    void mainFunc(vector<Point> points, Point origin) {
        ori = origin;
        priority_queue<Point, vector<Point>> pq(Comparator(ori));
                                             // ^^^^^^^^^^^^^^^ 
                                             // Pass an instance of 
                                             // the functor class
        for (int i = 0; i < points.size(); i++) {
            pq.push(points[i]);
        }
    }
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

静态嵌套类可以访问封闭类的哪些成员?

在Java嵌套类中,封闭类可以访问内部类的私有成员吗?

如何访问对象中的类成员变量(也是类的成员)

如何从Java中的嵌套类访问父类成员?

c++ - 如何从所有成员在c ++中都是公共的封闭类访问嵌套类成员函数?

如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?

访问嵌套类中的变量

封闭类可以访问嵌套类吗?

为什么嵌套类的私有成员可以被封闭类的方法访问?

在 CPP 中访问嵌套类成员函数

如何在Spring Boot中从SpringApplication访问类成员变量

内部类成员可从封闭类访问

如何访问封闭类的实例?

在Python中访问类的成员变量?

如何访问作为类成员变量的对象向量的成员变量?

如何使用数组对象访问嵌套类中的变量?

嵌套类中的静态成员对封闭类是否具有静态持续时间?

在C ++中,如何从指向驱动类的指针访问嵌套类的成员

模板类中嵌套类的C++静态成员变量

访问公共嵌套类的成员

如何在片段类中访问 MainActivity 类的公共成员(变量或方法)

如何访问模型类中的类变量

嵌套类<List>:如何访问列表中的嵌套类?

如何从成员访问受保护/私有嵌套类指针

为什么匿名类不能访问其封闭类的变量?

在C ++中,如何在继承的类中访问私有基本成员变量?

为什么嵌套类不能具有成员类型是封闭类的成员?

如何访问嵌套类的成员,该成员被外部类的成员隐藏

访问C#模板类中的成员变量