Reference to non-static member function must be called! Reference to non-static member function must be called!

The code is as follows:

class Solution { public: TreeNode* KthNode(TreeNode* pRoot, int k) { fun(pRoot); Int len = num. Size (); if(len == 0 || k > len) return NULL; sort(num.begin(),num.end(),com); // sort for(int I =0; i<k; i++) { if(i == k-1) return num[k-1]; } return NULL; Void fun(TreeNode* pRoot) {stack<TreeNode*> s; // TreeNode* pNode = pRoot; while(pNode) { s.push(pNode); num.push_back(pNode); PNode = pNode->left; } while(! s.empty()) { TreeNode* tempNode = s.top(); // select s.pop(); TreeNode* t = tempNode->right; TreeNode* t = tempNode->right; while(t) { num.push_back(t); // save the right subtree of the current node. t = t->left; }}}} static bool com(TreeNode* p1,TreeNode* p2) {return p1->val < p2->val; } public: vector<TreeNode*> num; // store sequential traversal};Copy the code

The problem is that you refer to (or call) a non-static function, but you do not call it from a class object. The source of the problem is the third predicate parameter of sort (). Why is this so?

Normally, non-static const member functions of the same class can call each other without accessing the class object. Why not here? Conversely, if we define the predicate function com () as static, the problem goes away.

The ** problem is caused by the function parameter mismatch problem. Because our ordinary member function has an implicit this pointer, com () on the surface of our predicate function only two arguments, but in fact it has three parameters, and we call sort sort () function compares two parameters need only used when, so there is a form to participate in the argument does not match the situation (function has three parameters, But only two arguments are entered.

So, the solution is to define the predicate function com () as a static member function. 支那