Possible Duplicate:
Can inner classes access private variables?
So I'm trying to use a priority queue, and in the context of this queue I want to define an integer i to be "less" than another integer j if D[i] < D[j]. How can I do this? (D is a data member of an object)
So far I have
/* This function gets the k nearest neighbors for a user in feature
* space. These neighbors are stored in a priority queue and then
* transferred to the array N. */
void kNN::getNN() {
int r;
priority_queue<int, vector<int>, CompareDist> NN;
/* Initialize priority queue */
for (r = 0; r < k; r++) {
NN.push(r);
}
/* Look at the furthest of the k users. If current user is closer,
* replace the furthest with the current user. */
for (r = k; r < NUM_USERS; r++) {
if (NN.top() > r) {
NN.pop();
NN.push(r);
}
}
/* Transfer neighbors to an array. */
for (r = 0; r < k; r++) {
N[r] = NN.top();
NN.pop();
}
}
And in kNN.hh:
class kNN {
private:
struct CompareDist {
bool operator()(int u1, int u2) {
if (D[u1] < D[u2])
return true;
else
return false;
}
};
...
However, this is giving me the error
kNN.hh: In member function ‘bool kNN::CompareDist::operator()(int, int)’:
kNN.hh:29: error: invalid use of nonstatic data member ‘kNN::D’
What can I do about this? It seems that C++ doesn't like it if I refer to specific objects in the comparator, but I have no idea how to solve this without referring to D.
Thanks!