Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Nested class: how to access the member variable in enclosed class

ROBOT AI:

The following code snippet aims to store all points in the member function mainFunc of enclosing class Solution in a priority_queue (i.e. pq), such that all points are in the order according to their distance to origin. However, the compiler reports an error:

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

Then I change the 3rd line of Point ori to static Point ori and change ori to Solution::ori in the function of distance(Point p), the link error occurs:

undefined reference to 'Solution::ori'

Could anyone help me on this? Thanks in advance!


/**
* 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 points, Point origin) {
ori = origin;
vector results;
priority_queue, Comparator> pq;
for (int i = 0; i pq.push(points[i]);
}
}
};



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Nested class: how to access the member variable in enclosed class

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×