错误:必须调用对非静态成员函数的引用

error: reference to non-static member function must be called

本文关键字:静态成员 函数 引用 调用 错误      更新时间:2023-10-16

编译时,我收到了这个

error: reference to non-static member function must be called
                        std::sort(sortedCOPG_.begin(),sortedCOPG_.end(),comp_copgNode); 

有人能让我做错事吗?我在没有类的情况下尝试了几乎相同的代码,但代码没有问题。这只是在我将代码封装在一个类中之后。为了简单起见,我在这里发布之前删除了很多代码。我试过其他类似的帖子,但都说不通

#include <fstream>
#include <set>
#include <unordered_map>
class Computation {
    public:
         Computation() {
        }
    private:
        struct copgNode_ {
            double weight;
            int source;
            int target;
        };

        template <typename Iter>
        void mst_score__( Iter first, Iter last) {
            Iter current = first;
            for (; current != last; ++current) {
                for (int i=0; i < n_; ++i) {
                    if (adjMatrix_[*current][i] != 0 ) sortedCOPG_.push_back(copgNode_{adjMatrix_[*current][i],*current,i});
                }
            }
            //Here, is the line causing errors---
            std::sort(sortedCOPG_.begin(),sortedCOPG_.end(),comp_copgNode);
        }
        void m_init_computation__() {
            std::vector<int> mst_test_vec{0,3,7};
            mst_score__(mst_test_vec.begin(),mst_test_vec.end());
    }

        bool comp_copgNode ( const copgNode_& left, const copgNode_& right) {return left.weight < right.weight;}    
};          

发现错误,必须替换compare函数。因为,它会让排序函数来识别自定义比较器覆盖默认比较器

friend bool operator< ( const copgNode_& left, const copgNode_& right) {return left.weight < right.weight;}